简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM