Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force a PowerShell script to be run as administrator

Tags:

powershell

I have written a powershell script that installs and starts some services. It will only work if the script is run as administrator. Is there any way force a script to have those privileges?

like image 333
Tom Squires Avatar asked Oct 05 '12 15:10

Tom Squires


People also ask

How do I force PowerShell to run as administrator?

Step 1: Open the Command Prompt, and type the PowerShell as a command, then press Enter key. Step 2: Now, the command prompt will turn to Windows PowerShell. Step 3: Type the command start-process PowerShell -verb runas and press "enter" key. Step 4: It will bring up an elevated Windows PowerShell as an administrator.

How do I run a PowerShell script with elevated privileges?

The easiest way to start elevated Powershell windows is by searching for the Powershell application. Press the Windows button to open the start menu and type Powershell. Select Run as administrator to launch run a Powershell window with full privileges. Press Yes in the UAC prompt, and you are good to go!

How do I run a remote PowerShell script as administrator?

The only way to get a remote PowerShell session to execute elevated (with admin privileges) is to connect with a user account (either implicitly or via -Credential ) that has admin privileges on the target machine. With such an account, the session automatically and invariably runs elevated.


1 Answers

In the case you are using PowerShell V2 You can also do the following :

Start-Process "$psHome\powershell.exe" -Verb Runas -ArgumentList '-command "Get-Service"'

This would run "Get-Service" as administrator, you can replace it with your script.

like image 85
JPBlanc Avatar answered Nov 08 '22 08:11

JPBlanc