The Powershell cmdlet Start-Process
is acting weirdly:
When I launch another console process and I specify -NoNewWindow
, the ExitCode property (an int
!) is null.
Here's a test: Same with something else besides cmd
. This test was on a Win10 with PS5, it is also the same with Win7 and PS5:
PS C:\Users\Martin> cmd.exe /Cver
Microsoft Windows [Version 10.0.15063]
PS C:\Users\Martin> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.15063.296
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.15063.296
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
PS C:\Users\Martin> $pNewWindow = Start-Process -FilePath "cmd.exe" -ArgumentList '/C"exit 42"' -PassThru
PS C:\Users\Martin> $pNewWindow.WaitForExit()
PS C:\Users\Martin> $pNoNewWindow.HasExited
True
PS C:\Users\Martin> $pNewWindow.ExitCode
42
PS C:\Users\Martin> $pNoNewWindow = Start-Process -FilePath "cmd.exe" -ArgumentList '/C"exit 42"' -PassThru -NoNewWindow
PS C:\Users\Martin> $pNoNewWindow.WaitForExit()
PS C:\Users\Martin> $pNoNewWindow.HasExited
True
PS C:\Users\Martin> $pNoNewWindow.ExitCode
PS C:\Users\Martin> $pNoNewWindow.ExitCode -eq $null
True
PS C:\Users\Martin> $pNoNewWindow | Get-Member | ? {$_.Name -imatch "exit"}
TypeName: System.Diagnostics.Process
Name MemberType Definition
---- ---------- ----------
Exited Event System.EventHandler Exited(System.Object, System.EventArgs)
WaitForExit Method bool WaitForExit(int milliseconds), void WaitForExit()
ExitCode Property int ExitCode {get;}
ExitTime Property datetime ExitTime {get;}
HasExited Property bool HasExited {get;}
PS C:\Users\Martin>
... So, the Property is there, but it's null
, even though it's an int
?