user2746059
user2746059

Reputation: 63

How to prompt to run EXE as different user in powershell

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

Answers (4)

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:

  1. Credentials in the form of <user>@<domain> don't work for some reason. you have to use the old Windows NT format.
  2. If you don't specify the 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

G T
G T

Reputation: 11

change directory to .exe directory and run below command

runas /netonly /user:<domain\username> .\<app name>

Upvotes: 0

Jmz
Jmz

Reputation: 11

Try following

Start-Process notepad.exe -Verb RunAsUser

Upvotes: 0

Nick
Nick

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

Related Questions