Home > front end >  Double-Click Execute as Administrator
Double-Click Execute as Administrator

Time:10-27

I have a powershell file saved to my desktop that I'd like to be able to double-click on and run as administrator. I've tried updating the shortcut used to launch powershell to always run as admin, but I still get the same permission issue. Is there a way to setup the shortcut so that the file always executes as admin? Or another way to set it up so that all powershell scripts run as admin when double-clicked?

Thanks for any help!

CodePudding user response:

Create a "proxy script" to launch other scripts as admin with, and then launch PowerShell using Start-Process -Verb RunAs to elevate it before execution:

RunAsProxy.ps1

# First arg should be the script path
$script = $args[0]

# Rest of args should be any script parameters
$scriptArgs = $args[1..$args.Count] -join ' '

$startProcessArgs = @{
  Wait = $true
  Verb = 'RunAs'
  FilePath = 'powershell'
  ArgumentList = "-File `"$script`" $scriptArgs"
}
Start-Process @startProcessArgs

exit $LASTEXITCODE

Create your shortcut to point to RunAsProxy.ps1 instead of the target script. Pass in the path to the target script you want to run elevated and optionally any parameters to that script. If UAC is enabled, you should get prompted to elevate before execution.

This should be re-usable with other scripts you want to do the same with.

  • Related