There are times when I want to elevate a PowerShell process on one of our lab Windows 7 clients & run a script as a user account with administrator privileges.
Usually I’m working on a lab machine running as a non-administrative test user. Since you cannot easily elevate and run as a different user from the right-click context menu, I wrote a simple script that I thought I’d share.
Note: One thing to call out is that our lab machines have their Execution Policy set for Unrestricted.
Start-Process powershell.exe -Credential "TestDomain\Me" -NoNewWindow -ArgumentList "Start-Process powershell.exe -Verb runAs"
The following section starts the PowerShell command-line process with Start-Process prompting for user credentials. You may not need this dependent on UAC settings, as you might already get an over-the-shoulder prompt for creds during elevation.
Start-Process powershell.exe -Credential "TestDomain\Me"
The -NoNewWindow parameter re-uses the same PowerShell command window.
Here I’m using -ArgumentList parameter to pass in the second Start-Process leveraging –Verb runAs to force the elevation prompt.
I just saved this PowerShell script to a scratch share on the lab machines, and when I need to elevate and run PowerShell as a different user. I’d just double click on the script. It’s not the most elegant code, but it gets the job done and ideally shows some fairly cool optional parameters of Start-Process.