Powershell Start-Process -NoNewWindow not working with -Credentials

Viewed 3473

I am looking to run an exe from powershell using a credential. I want the output to be in the same window. This is how my powershell looks.

Start-Process documentation: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-6

$username = 'user'
$password = 'password'
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
$pathNew = "c:\pathtomyexe\text.exe" 
Start-Process $pathNew -NoNewWindow -Credential ($credentials) -PassThru -Wait

With -Credential ($credentials) a new window is launched.

When I run Start-Process without -Credential, I get result in the same window as expected.

Start-Process $pathNew -NoNewWindow -PassThru -Wait

What am I doing wrong? Any pointers?

1 Answers

Short answer, you aren't doing anything wrong. You just won't be able to do this.

When you run Start-Process -NoNewWindow without -Credential you say: with the current already authenticated credentials, run the executable, and return the results within the same console window.

When you run Start-Process with -Credential the first question is: how do you verify that the Credentials are valid? You can't just look at the username and assume that you can re-use the existing session (e.g. the password might be wrong). To validate the credentials, Start-Process launches a new process as the username/password provided in the Credential object. It performs an authentication check and get a new authentication ticket.

Since it's a new process, running under a completely new context, with new authentication ticket, it ignores the -NoNewWindow flag as there is no way for the current console to redirect the output of the new process, and launches it as a new window.

Related