Run powershell script as administrator and with a different user account

Lee Exothermix picture Lee Exothermix · Apr 5, 2018 · Viewed 16k times · Source

I need to use powershell to launch a powershell script as administrator and as a different user than the currently logged in user. This is for running some updates to GPO on several server machines. The intent is for the script to prompt for credentials when it launches.

Here is what I have tried so far:

 Start-Process 'powershell.exe' -Credential 'adminAccount' -ArgumentList '"file path\updateGPO.ps1" -Verb runAs'

This prompts for a password and will run the script with the specified account but doesn't run as administrator, giving an 'Access is Denied' error for administrator actions. The new powershell window is missing the Administrator text in the title.

I also tried the following:

 $script = 'file path\updateGPO.ps1'
 Start-Process 'powershell.exe' -Credential 'adminAccount' -ArgumentList '-command &{start-process $script} -Verb runAs'

This launches a powershell window, writes no text, and immediately closes.

Thanks in advance for any insights.

Answer

Lee Exothermix picture Lee Exothermix · Apr 5, 2018

The comment from TheIncorrigible1 put me on the right track.

 Start-Process 'powershell.exe' -Credential 'adminAccount' -ArgumentList '"file path\updateGPO.ps1"'

At the top of the updateGPO.ps1 script I included the self-elevating mechanism.