A common idiom (in the Bash world, which inspired PowerShell's && and || operators) is to conditionally exit a script when invocation of a command fails, along the lines of:
# Assume existence of /somepath and exit, if it doesn't exist.
ls /somepath || exit 1
Currently, neither exit nor return nor throw can be used on the RHS of && / ||
Steps to reproduce
{ Get-Item /nosuch || return } | Should -Not -Throw
{ Get-Item / && return } | Should -Not -Throw
Expected behavior
The tests should pass.
Actual behavior
The tests fail, because return is not recognized
Expected no exception to be thrown, but an exception
"The term 'return' is not recognized as the name of a
cmdlet, function, script file, or operable program. ..."...
Environment data
PowerShell Core 7.0.0-preview.6
A common idiom (in the Bash world, which inspired PowerShell's
&&and||operators) is to conditionally exit a script when invocation of a command fails, along the lines of:Currently, neither
exitnorreturnnorthrowcan be used on the RHS of&&/||Steps to reproduce
{ Get-Item /nosuch || return } | Should -Not -Throw { Get-Item / && return } | Should -Not -ThrowExpected behavior
The tests should pass.
Actual behavior
The tests fail, because
returnis not recognizedEnvironment data