Powershell Run Bat File As Administrator



Why call my PowerShell script from a batch file. You can’t double-click to run.PS1 files, but you can execute a.BAT file that way. So, we’ll write a batch file to call the PowerShell script from the command line. There are several good reasons for doing this: Non-technical Users are often tackled with PowerShell. Hi, I was wondering if it is possible to create a powershell script that uses the start-process command allowing me to open up a program (lets say cmd) as the administrator? The reason is I want the ‘do you want to make changes to your pc.’ sign to come up, in order for me to try use send keys to type in the password. When using Powershell, you may need to run Powershell as an administrator to perform a specific task. The reason for this is the User Account Control (UAC). Introduced with Windows Vista User Account Control (UAC) keeps the user in a non-elevated state if not explicitly told to be elevated as an administrator.

There are several ways to run a PowerShell script.

Before running any scripts on a new PowerShell installation, you must first set an appropriate Execution Policy,
e.g. Set-ExecutionPolicy RemoteSigned

If the script has been downloaded from the internet and saved as a file then you may also need to right click on the script, select properties, then unblock. If you just copy and paste the text of the script, this is not needed.

If I finally cave in and choose to launch the shortcut with 'Run as Administrator', the script executes OK - but that merely triggers the very UAC prompt I'm trying to avoid. The account I'm using is an administrator. My script, amongst other things, executes runas.exe to launch a.EXE file under a second admin account. Any bright ideas? Step #3 states to run the script with admin privileges. If this is not set then the whole thing won't run with administrator rights and the script will fail. If you tested the batch file using the command line then you will have to run the command line with admin privileges as well, though that has nothing to do with Step #3.

A PowerShell script is the equivalent of a Windows CMD or MS-DOS batch file, the file should be saved as plain ASCII text with a .ps1 extension, e.g. MyScript.ps1

Call or Invoke a script to run it

The most common (default) way to run a script is by callingit:

PS C:> & 'C:BatchMy first Script.ps1'

PS C:> & cscript /nologo 'C:Batchanother.vbs'

If the path does not contain any spaces, then you can omit the quotes and the '&' operator

Bat

PS C:> C:BatchMyscript.ps1

Zydas network & wireless cards driver. If the script is in the current directory, you can omit the path but must instead explicitly indicate the current directory using . (or ./ will also work)

PS C:> .Myscript.ps1

An important caveat to the above is that the currently running script might not be located in the current directory.

Call one PowerShell script from another script saved in the same directory:

#Requires -Version 3.0
& '$PSScriptRootset-consolesize.ps1' -height 25 -width 90

When you invoke a script using the syntax above, variables and functions defined in the script will disappear when the script ends.1

An alternative which allows running a script (or command) on local or remote computers is Invoke-Command

PS C:> invoke-command -filepath c:scriptstest.ps1 -computerName Server64

1unless they are explicitly defined as globals: Function SCOPE:GLOBAL or Filter SCOPE:GLOBAL or Set-Variable -scope 'Global'

Run a PowerShell Script from the GUI or with a shortcut

This can be done by running PowerShell.exe with parameters to launch the desired script.

Run As Administrator (Elevated)

See the PowerShell elevation page for ways of running a script or a PowerShell session 'As admin'

Dot Sourcing

When you dot sourcea script, all variables and functions defined in the script will persist even when the script ends.

Run a script by dot-sourcing it:

Powershell run command

PS C:> . 'C:BatchMy first Script.ps1'

Dot-sourcing a script in the current directory:

PS C:> . .Myscript.ps1'

Run a CMD batch file

Run a batch script from PowerShell:
PS C:> ./demo.cmd
Early versions of PowerShell would only run internal CMD commands if the batch file was run by explicitly calling the CMD.exe shell and passing the batch file name.

Run a single CMD internal command

This will run the CMD.exe version of DIR rather than the powershell DIR alias for Get-ChildItem:

Open Powershell With Batch File

PS C:> CMD.exe /C dir

Run a VBScript file

Run a vb script from PowerShell:
PS C:> cscript c:batchdemo.vbs

The System Path

Powershell Run Bat File As Administrator Default

If you run a script (or even just enter a command) without specifying the fully qualified path name, PowerShell will search for it as follows:

  1. Currently defined aliases
  2. Currently defined functions
  3. Commands located in the system path.

Read A Bat File From Powershell

#Yeah, I'm gonna run to you, cause when the feelin's right I'm gonna stay all night, I'm gonna run to you# ~ Bryan Adams

Run multiple powershell commands from batch

Related PowerShell Cmdlets:

Powershell

#requires - Prevent a script from running without a required element.
Basic PowerShell script Template - HowTo.
Invoke-Command - Run commands on local and remote computers.
Invoke-Expression - Run a PowerShell expression.
Invoke-Item - Invoke an executable or open a file (START).
The call operator (&) - Execute a command, script or function.
Set-Variable - Set a variable and its value.
Functions - Write a named block of code.
CMD Shell: Run a PowerShell script from the CMD shell.
VBScript: Run a script from VBScript

Copyright © 1999-2020 SS64.com
Some rights reserved