# Nested ForEach Alternatives?

**URL:** <https://forums.powershell.org/t/nested-foreach-alternatives/6039>\
**Category:** PowerShell Help\
**Created:** [March 20, 2016, 8:52am UTC](https://forums.powershell.org/t/nested-foreach-alternatives/6039 "2016-03-20T08:52:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![signalwarrant](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/signalwarrant/32/508_2.png) [@signalwarrant](https://forums.powershell.org/u/signalwarrant)\
**Post date:** [March 20, 2016, 8:52am UTC](https://forums.powershell.org/t/nested-foreach-alternatives/6039/1 "2016-03-20T08:52:01Z")

</div>

I need to run the code below on about 200 client machines. The code works as I intended, but I’m wondering if there is a more efficient way to do this without using nested ForEach loops? Nesting loops just doesn’t seem efficient to me.

Any suggestions?

```
foreach ($Computer in $ComputerName){
   
   ############ Java Product Info
   
   $JavaVer = Get-ChildItem "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" -Recurse |
                ForEach-Object { Get-ItemProperty $_.pspath } | Where-Object DisplayName -Like "*Java 8*" | Select-Object DisplayVersion
}
```

---

<div class="post-metadata">

**Author:** ![silent-steel](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@silent-steel](https://forums.powershell.org/u/silent-steel)\
**Post date:** [March 20, 2016, 9:19am UTC](https://forums.powershell.org/t/nested-foreach-alternatives/6039/2 "2016-03-20T09:19:54Z")

</div>

Using Workflow and the -Parallel could be one option:

[https://technet.microsoft.com/en-us/library/jj713711.aspx](https://technet.microsoft.com/en-us/library/jj713711.aspx)  
[https://technet.microsoft.com/en-us/library/jj574157.aspx](https://technet.microsoft.com/en-us/library/jj574157.aspx)

Using Invoke-Parallel might be another:

[https://gallery.technet.microsoft.com/scriptcenter/Foreach-Parallel-Parallel-a8f3d22b](https://gallery.technet.microsoft.com/scriptcenter/Foreach-Parallel-Parallel-a8f3d22b)

[http://serverfault.com/questions/626711/how-do-i-run-my-powershell-scripts-in-parallel-without-using-jobs](http://serverfault.com/questions/626711/how-do-i-run-my-powershell-scripts-in-parallel-without-using-jobs)

Using jobs on all your nodes might be a third  
(not the most used as it has its issues but still a possibility)  
[http://stackoverflow.com/questions/4016451/can-powershell-run-commands-in-parallel](http://stackoverflow.com/questions/4016451/can-powershell-run-commands-in-parallel)

---

<div class="post-metadata">

**Author:** ![signalwarrant](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/signalwarrant/32/508_2.png) [@signalwarrant](https://forums.powershell.org/u/signalwarrant)\
**Post date:** [March 23, 2016, 9:40am UTC](https://forums.powershell.org/t/nested-foreach-alternatives/6039/3 "2016-03-23T09:40:56Z")

</div>

In my use case scenario, is it safe to say the performance difference in all those options are probably negligible at best? I know I can use Measure-Command, but how accurate a measure of performance is that?

---

<div class="post-metadata">

**Author:** ![flynn-bundy](https://avatars.discourse-cdn.com/v4/letter/f/c57346/32.png) [@flynn-bundy](https://forums.powershell.org/u/flynn-bundy)\
**Post date:** [March 23, 2016, 11:54pm UTC](https://forums.powershell.org/t/nested-foreach-alternatives/6039/4 "2016-03-23T23:54:28Z")

</div>

Something like:

```
$JavaVer = Invoke-Command $ListOfComputerNames {
     Get-ChildItem "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" -Recurse |
     ForEach-Object { Get-ItemProperty $_.pspath } | Where-Object DisplayName -Like "*Java 8*" | Select-Object DisplayVersion
}
```

Would also work just fine. Invoke-Command is going to be a lot faster than using a foreach loop with 200 computers. Invoke-Command takes an array of strings also so you can simply just give it every machine name at the same time. Give it a shot. Let us know how you go.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:41pm UTC](https://forums.powershell.org/t/nested-foreach-alternatives/6039/5 "2024-05-16T20:41:44Z")

</div>

Closed on May 16, 2024, 8:41pm UTC


