Home > Enterprise >  "Break" causes a PowerShell script to fail
"Break" causes a PowerShell script to fail

Time:04-01

(GSV).Name|%{If($_ -like "*net*"){ $_ ; Break }};Pause

When I run the above line from a PowerShell window, "Netlogon" will be output.
When I run a PS1 file that contains the same line, a PowerShell window will appear and then disappear immediately, and nothing will be output.
What is wrong with that line?

CodePudding user response:

From the about_Break help topic:

Do not use break outside of a loop, switch, or trap

When break is used outside of a construct that directly supports it (loops, switch, trap), PowerShell looks up the call stack for an enclosing construct. If it can't find an enclosing construct, the current runspace is quietly terminated.

This means that functions and scripts that inadvertently use a break outside of an enclosing construct that supports it can inadvertently terminate their callers.

Using break inside a pipeline break, such as a ForEach-Object script block, not only exits the pipeline, it potentially terminates the entire runspace.

Rewrite your script to use Where-Object to filter the input, then use Select-Object -First 1 to terminate the pipeline once an object matches the filter:

(GSV).Name |Where-Object { $_ -like "*net*" } |Select-Object -First 1; Pause

In this particular case you can also use Get-Service's filtering capabilities to narrow the initial query to only matching services, voiding the need for Where-Object all together:

Get-Service -Name *net* |Select -ExpandProperty Name -First 1
  • Related