27 March 2013

How to run a Powershell script as administrator

Posted By: Mick Pletcher - 3:21 PM


& Comment

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.

About Mick Pletcher

My blog is here to help solve issues I have encountered and solved, publish scripts I have written, and educate others in understanding areas that are not well covered


Copyright © 2013 Mick's IT Blogs™ is a registered trademark.