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?

1
  • 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. Commented Mar 31, 2022 at 14:02

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.