How to elevate permissions for your shell within a script

At one time another you will right a script where you need to have it run from an elevated shell and there are many ways you can do this but today I'm going to focus on the following situation.

"I want to run a script thru Task Scheduler with an elevated shell so that I can schedule it, but do not want to disable UAC"

So your going to want to create a batch file, and in that batch file you will have the following line:

Powershell –command Start-Process “$PSHome\PowerShell.exe” –Verb RunAs -WindowStyle Hidden –ArgumentList ‘-file <Path and File Name.ps1>’ -Wait

NOTE: There are other switches you may want to use with this command and of course you will need to change <Path and File Name.ps1> to something that makes sense.

So what this tells Windows to do is run a PowerShell Command in this case it will be Start-Process. Then using this command we open a PowerShell prompt and because we use the switch -Verb with RunAs it will open an elevated prompt getting around UAC.

As well I use the -WindowStyle Hidden so that there is no attempt to show a prompt and the switch -ArgumentList which will feed the script name to PowerShell.exe

Finally we have the switch -Wait which will tell the command to wait till the script is done running before moving on.

So now that you have your batch file created all you have to do is setup Task Scheduler with an account that has the correct permissions and away we go.