Enigmoramous
Enigmoramous

Reputation: 23

Run a Powershell script from Batch file with elevated privileges?

I want to run example.ps1 from code.bat

I realize that the start command would run it but require that it be run as admin, how can I do this?

Upvotes: 1

Views: 14198

Answers (1)

henrycarteruk
henrycarteruk

Reputation: 13227

In your batch file:

powershell -Command "&{ Start-Process powershell -ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs}"

...basically you're using Powershell to run Powershell. The second instance is elevated to Admin and is running your script with those permissions.


Full explanation:

Start-Process can be used to run a program, and also has the parameter -Verb RunAs which elevates the program to run as Admin.

We can't call Start-Process from a batch file as it's a PowerShell command.

But we can run powershell from a batch file, then using the -command parameter to run Start-Process.

We use Start-Process to run powershell (again) and run your script when it is elevated to Admin: -ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs

Upvotes: 4

Related Questions