1
(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?

Matthew Wai
  • 962
  • 7
  • 14
  • Mathias' answer explains the problem well and offers the best solution for your specific use case. Unfortunately, as of PowerShell 7.2.2 there still is no _general_ mechanism for _exiting a pipeline on demand_ - see [this answer](https://stackoverflow.com/a/60341948/45375). – mklement0 Mar 31 '22 at 14:02

1 Answers1

2

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
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206