Skip to content

Suppressing stderr output with 2>$null or redirecting it to a file causes stderr lines to be recorded in $Error #11133

@mklement0

Description

@mklement0

Closely related: #4002; see also: #4332

By design, stderr output from external programs is not recorded in $Error by default.

Paradoxically, when you use 2>$null in order to explicitly suppress stderr output, it then is recorded in $Error, which is obviously not the intent.

Note: It also happens when you redirect to an actual file.

Steps to reproduce

$Error.Clear()
# Call `whoami` with an invalid argument, which produces stderr output (all platforms)
whoami badarg 2>$null
$Error.Count | Should -Be 0

Expected behavior

The test should succeed.

Actual behavior

The test fails with something like:

Expected 0, but got 1

That is, stderr output was unexpectedly recorded in $Error.

Environment data

PowerShell Core 7.0.0-preview.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions