PowerShell Return behavior in PowerShell Gotcha! Return in the pipeline


Example

get-childitem | foreach-object { if ($_.IsReadOnly) { return } } 

Pipeline cmdlets (ex: ForEach-Object, Where-Object, etc) operate on closures. The return here will only move to the next item on the pipeline, not exit processing. You can use break instead of return if you want to exit processing.

get-childitem | foreach-object { if ($_.IsReadOnly) { break } }