troubleshooting Question

PowerShell run commands once in nested foreach

Avatar of Kelly Garcia
Kelly GarciaFlag for United Kingdom of Great Britain and Northern Ireland asked on
9 Comments2 Solutions75 ViewsLast Modified:

I have a code similar to the below:

foreach($a in @(1..3)){
echo $a
foreach ($b in @("a","b","c","d","e")){
echo "Test"



i want the echo "test" to only happen once within the first foreach.  my real code contains powershell commandlets and i only want to execute these commandlets once per 1..3. so first loop will output 1, then it will go to the next foreach each which gives value a. within a, b, c, d, e. i want to echo test, however when it goes to 2 which is the first foreach, i dont want to echo test anymore. i hope this makes sense.  How can this be achieved?
Join our community to see this answer!
Unlock 2 Answers and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros