PowerShell - Return 未按预期运行

PowerShell - Return doesn't behave as expected

我正在使用 PowerShell v5.1。 我有一个关于 "Return" 的问题。 "Return" 不应该停止脚本执行吗?

Get-CimInstance Win32_OperatingSystem | ForEach-Object {
    "Exiting"
    Return
}
"It doesn't"

ForEach ($number in 1..2){
    "Exiting"
    Return
}
"It doesn't"

结果是:退出 => 不 => 退出

如您所见,"Return" 在 "ForEach-Object" 循环中的行为与在 "ForEach" 循环中的行为不同。我错过了什么?谢谢:-)

根据手册,https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_return?view=powershell-5.1

添加了重点:

退出当前范围,可以是函数、脚本或脚本块。

只要您点击 Return 关键字,它就会退出当前正在使用的括号。