27 March 2013

How to run a Powershell script as administrator

Say you are deploying a package and need to run it as admin with elevated privileges. Of course if you are running the script manually, you can open up powershell with admin priviledges by running the cmd.exe as administrator. I haven't been able to find any easy ways of doing this within Powershell scripts, although it can be done. There is an easy way though if you use VBScript as a Powershell wrapper. Create the VBScript and point it to execute the Powershell script as admin. Here is how:

 Set oShell = CreateObject("Shell.Application")  
 oShell.ShellExecute "powershell", "-executionpolicy bypass -file test.ps1", "", "runas", 1  

That is all that is to it. Execute the VBScript and the Powershell script will be executed as administrator.