You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
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
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.
changed the title [-]Write-Error doesn't set $? to $False (doesn't reflect the execution status)[/-][+]Write-Error doesn't set $? to $False (doesn't set the execution status to indicate a non-terminating error)[/+]on Apr 23, 2017
changed the title [-]Write-Error doesn't set $? to $False (doesn't set the execution status to indicate a non-terminating error)[/-][+]Write-Error in a function doesn't set $? to $False (doesn't set the execution status to indicate a non-terminating error) in the caller's scope[/+]on Apr 23, 2017
This issue is about the ability for an (advanced) function, as a functional unit, to signal to the caller that a non-terminating error has occurred.
While you can do that with $PSCmdlet.WriteError(), it is quite cumbersome.
What I'm suggesting is that Write-Error similarly set $? to $False in the caller's scope on returning from the function. I've updated the title and first post to clarify that.
Making the variable -Option AllScope is problematic, because it would restrict use of Write-Error to the very last statement of the function / script (and Write-Error itself would then have to not reflect its success in $?), so that subsequent statements don't overwrite it.
$PSCmdlet.WriteError() already works as expected:
it sets $? in the caller's scope, wherever it is used in the script / function's body
while leaving the local copy untouched, so that the remainder of the script / function can continue to make use of it to determine the success of calling other commands.
I haven't looked at the code yet, but it sounds like it shouldn't be too hard to make Write-Error behave the same way.
@PowerShell/powershell-committee reviewed this. We agree there is a need for a script or pipeline to have a way to set $? but should not change the current behavior of Write-Error which would be a breaking change. Proposals were discussed in #10917
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 23, 2023
Activity
[-]Write-Error doesn't set $? to $False (doesn't reflect the execution status)[/-][+]Write-Error doesn't set $? to $False (doesn't set the execution status to indicate a non-terminating error)[/+]iSazonov commentedon Apr 23, 2017
As discussed in #3359 here "$?" return status of last statement. So the behavior is by design.
Michael Klement (@mklement0) Please correct typo in "Steps to reproduce"
[-]Write-Error doesn't set $? to $False (doesn't set the execution status to indicate a non-terminating error)[/-][+]Write-Error in a function doesn't set $? to $False (doesn't set the execution status to indicate a non-terminating error) in the caller's scope[/+]mklement0 commentedon Apr 23, 2017
Ilya (@iSazonov):
Re as-designed:
This issue is about the ability for an (advanced) function, as a functional unit, to signal to the caller that a non-terminating error has occurred.
While you can do that with
$PSCmdlet.WriteError(), it is quite cumbersome.What I'm suggesting is that
Write-Errorsimilarly set$?to$Falsein the caller's scope on returning from the function. I've updated the title and first post to clarify that.iSazonov commentedon Apr 23, 2017
Typo:
function foo { Write-Error 'oh no' }; $?vsfunction foo { Write-Error 'oh no' }; foo; $?mklement0 commentedon Apr 23, 2017
Ilya (@iSazonov) Ah, got it, thanks - updated.
BrucePay commentedon Apr 26, 2017
Perhaps $? should be all scope.
mklement0 commentedon Apr 26, 2017
Bruce Payette (@BrucePay):
Making the variable
-Option AllScopeis problematic, because it would restrict use ofWrite-Errorto the very last statement of the function / script (andWrite-Erroritself would then have to not reflect its success in$?), so that subsequent statements don't overwrite it.$PSCmdlet.WriteError()already works as expected:it sets
$?in the caller's scope, wherever it is used in the script / function's bodywhile leaving the local copy untouched, so that the remainder of the script / function can continue to make use of it to determine the success of calling other commands.
I haven't looked at the code yet, but it sounds like it shouldn't be too hard to make
Write-Errorbehave the same way.3 remaining items
SteveL-MSFT commentedon Jan 8, 2020
@PowerShell/powershell-committee reviewed this. We agree there is a need for a script or pipeline to have a way to set
$?but should not change the current behavior ofWrite-Errorwhich would be a breaking change. Proposals were discussed in #10917microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.