Running Powershell Scripts with Shortcuts and Administrator Priviliges

If you want to elevate a powershell to execute a script with admininstrator priviliges, I found this code snippet useful. Used as the first line in your script, it elevates the powershell and triggers a UAC prompt. The rest of the script then gets passed on to the elevated powershell instance.

if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File "$PSCommandPath"" -Verb RunAs; exit }

To run a powershell script with a double click, like with a .bat file, I created a shortcut with these arguments.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\Users\USERNAME\Documents\ps_scripts\SCRIPTNAME.ps1

This saves me from right clicking and clicking again on the context menu entry for running the script in powershell.