How to prompt to run EXE as different user in powershell
Asked Answered
L

4

6

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.

Lewiss answered 27/10, 2015 at 20:9 Comment(4)
"I'm having a hard time getting the runas command to work: please show as a edit to the question what you tried to make the runas command workNoman
Possible duplicate of Running PowerShell as another user, and launching a scriptGullet
Please give @Nick credit and mark it answered.Aficionado
It works in PowerShell commandline shell, because powershell can run most executables from the commandline. Start-Process and Invoke-Command are used to run commands (PS or external) as if they were run from the commandline.Aficionado
B
6

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
Breadbasket answered 28/10, 2015 at 2:10 Comment(0)
H
0

Try following

Start-Process notepad.exe -Verb RunAsUser
Halfmoon answered 19/5, 2020 at 9:6 Comment(0)
N
0

change directory to .exe directory and run below command

runas /netonly /user:<domain\username> .\<app name>
Netta answered 7/10, 2020 at 2:2 Comment(0)
F
0

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."
Fricandeau answered 27/2 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.