Reputation: 63
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.
Upvotes: 6
Views: 40770
Reputation: 3181
Don't judge me for my credential handling!
$Pwd=ConvertTo-SecureString -String "<password>" -AsPlainText -Force
$Cred=[System.Management.Automation.PSCredential]::new("<domain>\<username>",$Pwd)
Start-Process -FilePath "<path to .EXE>" -Credential $Cred -WorkingDirectory "~"
Why this worked:
<user>@<domain>
don't work for some reason. you have to use the old Windows NT format.WorkingDirectory
, PowerShell will use the current directory. So, if your target user doesn't have permissions there, you'll get the error, "Start-Process : This command cannot be run due to the error: The directory name is invalid.
"Upvotes: 0
Reputation: 11
change directory to .exe directory and run below command
runas /netonly /user:<domain\username> .\<app name>
Upvotes: 0
Reputation: 1863
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
Upvotes: 6