Skip to content

Write-Error in a function doesn't set automatic success variable $? to $False (doesn't set the execution status to indicate a non-terminating error) in the caller's scope #3629

@mklement0

Description

@mklement0

Write-Error is used to signal a non-terminating error, yet $? (the execution-status automatic variable) isn't set to $False in the caller's scope when it is called from an (advanced) function.

By contrast, if you use $PSCmdlet.WriteError from an advanced function, it is.

Steps to reproduce

function foo { Write-Error 'oh no' }; foo; $?
function foo2 { [cmdletbinding()] param() $PSCmdlet.WriteError((New-Object System.Management.Automation.ErrorRecord "oh no", $null, 'NotSpecified', $null)) }; foo2; $?

Expected behavior

# (error details omitted)
$False
# (error details omitted)
$False

Actual behavior

# (error details omitted)
$True
# (error details omitted)
$False

Environment data

PowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on Darwin Kernel Version 16.5.0: Fri Mar  3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking-Changebreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdlets-Corecmdlets in the Microsoft.PowerShell.Core moduleWG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions