How to prompt to run EXE as different user in powershell

user2746059 picture user2746059 · Oct 27, 2015 · Viewed 17.9k times · Source

How would I go about running an EXE as a different user? How could I prompt for credentials or atleast ask for the password for a local admin to launch an exe through powershell. I'm having a hard time getting the runas command to work.

This was the latest thing I tried: runas -credential .\me c:\windows\system32\notepad.exe

This works in the powershell terminal: runas /user:asdf c:\windows\system32\notepad.exe but doesn't ask for credentials in a standalone powershell script.

Answer

Nick picture Nick · Oct 28, 2015

This is a simple operation.

Start-Process "c:\windows\system32\notepad.exe" -Credential $(Get-Credential)

Using Get-Credential will prompt the user for credentials, You can also store it in a variable.

$Creds = Get-Credential
Start-Process "c:\windows\system32\notepad.exe" -Credential $Creds