Skip to content
mnaoumov.dev
Go back

PowerShell Start-Process WTF

Hi folks

Another WTF… Let’s try

$p = Start-Process -FilePath cmd -ArgumentList "/c exit 123" -PassThru
$p.ExitCode

And it returned nothing. As I described in the previous blogpost, this means that some exception occurred

$p.get_ExitCode()

returns

Exception calling "get_ExitCode" with "0" argument(s): "Process was not started by this object, so requested information cannot be dete rmined." At line:1 char:16 + $p.get_ExitCode <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

That is a known PowerShell bug: Start-Process: http://connect.microsoft.com/PowerShell/feedback/details/585549/start-process-passthru-return-value and http://connect.microsoft.com/PowerShell/feedback/details/520554/start-process-does-not-return-exitcode-property

And here is the workaround

$p = [System.Diagnostics.Process]::Start("cmd", "/c exit 123")
$p.ExitCode

returns 123


Share this post on:

Previous Post
PowerShell Start-Job WTF
Next Post
PowerShell .NET property access swallows exceptions