Skip to content

Start-Process does not populate the process object's exit code with NoNewWindow switch #20400

@unlikelier

Description

@unlikelier

Prerequisites

Steps to reproduce

I noticed that the Start-Process cmdlet sometimes does not populate the ExitCode property in the output, I believe it should.
This happed at least when -PassThru, -Wait, and -NoNewWindow switches are provided. -NoNewWindow seems to be the reason, since the exit code is available without it.

Steps to reproduce

You can see this behavior with this simple test script

Set-Content -Value "exit 1" -Path script.ps1
$p = start-process "pwsh" script.ps1 -PassThru -NoNewWindow -Wait
$p.ExitCode  # Should output: 1
$p = start-process "pwsh" script.ps1 -PassThru -Wait
$p.ExitCode # Outputs: 1

Expected behavior

PS C:\> set-content -Value "exit 1" -Path script.ps1
PS C:\> $p = start-process "pwsh" script.ps1 -PassThru -NoNewWindow -Wait
PS C:\> $p.ExitCode
1
PS C:\> $p = start-process "pwsh" script.ps1 -PassThru -Wait
PS C:\> $p.ExitCode
1

Actual behavior

PS C:\> set-content -Value "exit 1" -Path script.ps1
PS C:\> $p = start-process "pwsh" script.ps1 -PassThru -NoNewWindow -Wait
PS C:\> $p.ExitCode
PS C:\> $p = start-process "pwsh" script.ps1 -PassThru -Wait
PS C:\> $p.ExitCode
1

Error details

No response

Environment data

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.0-preview.5
PSEdition                      Core
GitCommitId                    7.4.0-preview.5
OS                             Microsoft Windows 10.0.20348
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.Resolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions