Gotcha Return in the pipeline
suggest changeget-childitem | foreach-object { if ($_.IsReadOnly) { return } }
ForEach-Object and Where-Object run a script block for each object. return in that script block only moves on to the next pipeline object. It does not stop the pipeline. break is not a safe substitute: if there is no enclosing loop, it can stop the rest of the script. Prefer Where-Object to filter, or a foreach statement when you need break. The break example below can exit more than the pipeline:
get-childitem | foreach-object { if ($_.IsReadOnly) { break } }
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents