Powershell start notepad.exe and a command windows starts and disappears

user1548815 picture user1548815 · Aug 21, 2014 · Viewed 47.8k times · Source

I have a script to start another ps1 script as aspecific user that does a simple dir on a directory and it works, when it runs a separate cmd window appears with the directory contents.

$encpwd = Get-Content C:\path\to\creds.txt

$passwd = ConvertTo-SecureString $encpwd

$cred = new-object System.Management.Automation.PSCredential 'theuser',$passwd

Start-Process c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -Credential $cred  -ArgumentList '-noexit','-File', 'C:\path\to\script\test.ps1'

My goal is to start an application as a specific user. So first I wanted to test with notepad.exe So I changed the last line to:

Start-Process c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe -Credential $cred  -ArgumentList '-noexit','-File', 'C:\windows\system32\notepad.exe'

All I get is a command window that quickly disappears, can't tell if there is an error etc. I also tried calling a ps1 script that invoked notepad and got the same result, even though on its own the the new script invoked notepad just fine.

Can someone explain where I am going wrong, Thanks

Answer

CitizenRon picture CitizenRon · Aug 21, 2014

If you're wanting to start an application, wouldn't you just use that as the target of Start-Process instead of opening another PowerShell window to do it?

Start-Process 'C:\windows\system32\notepad.exe' -Credential $cred