# Out-gridview with foreach

**URL:** <https://forums.powershell.org/t/out-gridview-with-foreach/5046>\
**Category:** PowerShell Help\
**Created:** [October 17, 2015, 6:26pm UTC](https://forums.powershell.org/t/out-gridview-with-foreach/5046 "2015-10-17T18:26:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike-eyler](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mike-eyler](https://forums.powershell.org/u/mike-eyler)\
**Post date:** [October 17, 2015, 6:26pm UTC](https://forums.powershell.org/t/out-gridview-with-foreach/5046/1 "2015-10-17T18:26:27Z")

</div>

Does anyone have a good way to take the results from a foreach loop and output the data using out-gridview?

---

<div class="post-metadata">

**Author:** ![jonathan-warnken](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jonathan-warnken/32/469_2.png) [@jonathan-warnken](https://forums.powershell.org/u/jonathan-warnken)\
**Post date:** [October 17, 2015, 9:15pm UTC](https://forums.powershell.org/t/out-gridview-with-foreach/5046/2 "2015-10-17T21:15:22Z")

</div>

A very simple example is

```
1..10|foreach{$_}|Out-GridView
```

The principle is each time the loop runs you return the data so the out-gridview can display it. For a better example see [http://mrbodean.azurewebsites.net/2015/10/06/get-the-source-location-for-all-packages-in-a-sccm-task-sequence/](http://mrbodean.azurewebsites.net/2015/10/06/get-the-source-location-for-all-packages-in-a-sccm-task-sequence/)  
Any function built to return data to the pipeline with be able to use the out-gridview cmdlet .

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [October 17, 2015, 10:35pm UTC](https://forums.powershell.org/t/out-gridview-with-foreach/5046/3 "2015-10-17T22:35:47Z")

</div>

Mike, it would be helpful to see your code if it’s not 100’s of lines long.

---

<div class="post-metadata">

**Author:** ![mike-eyler](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mike-eyler](https://forums.powershell.org/u/mike-eyler)\
**Post date:** [October 18, 2015, 4:05am UTC](https://forums.powershell.org/t/out-gridview-with-foreach/5046/4 "2015-10-18T04:05:11Z")

</div>

Jonathan, i get an error when I do this.

At line:6 char:3

- } | Out-GridView
- ~  
An empty pipe element is not allowed.
  - CategoryInfo : ParserError: (🙂 , ParentContainsErrorRecordException
  - FullyQualifiedErrorId : EmptyPipeElement

---

<div class="post-metadata">

**Author:** ![mike-eyler](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mike-eyler](https://forums.powershell.org/u/mike-eyler)\
**Post date:** [October 18, 2015, 4:09am UTC](https://forums.powershell.org/t/out-gridview-with-foreach/5046/5 "2015-10-18T04:09:09Z")

</div>

I figured it out:

$processes = (‘csrss’,‘svchost’,‘explorer’)

foreach($process in $processes){  
$Output += Get-Process -Name $process | select ProcessName,VM  
}  
$Output | Out-GridView

---

<div class="post-metadata">

**Author:** ![mike-eyler](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mike-eyler](https://forums.powershell.org/u/mike-eyler)\
**Post date:** [October 18, 2015, 4:39am UTC](https://forums.powershell.org/t/out-gridview-with-foreach/5046/6 "2015-10-18T04:39:42Z")

</div>

It doesn’t work like that with Get-WmiObject though…  
Any ideas?

$Server = (‘localhost’,‘Server1’,‘Server2’)

foreach($Server in $Servers){  
$Output += Get-WmiObject -Class Win32\_Networkadapter -ComputerName $Server | select PSComputerName,Manufacturer,Description,NetConnectionID,Speed,IPAddress,IPSubnet,DefaultIPGateway,DnsServerSearchOrder,DNSDomainSuffixSearchOrder  
}  
$Output | Out-GridView

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [October 18, 2015, 10:01am UTC](https://forums.powershell.org/t/out-gridview-with-foreach/5046/7 "2015-10-18T10:01:04Z")

</div>

Based on the code you posted, the problem is that your variable for your list of servers is missing an “s”

$Server = (‘localhost’,‘Server1’,‘Server2’)

Should be

$Servers = (‘localhost’,‘Server1’,‘Server2’)

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [October 18, 2015, 10:35am UTC](https://forums.powershell.org/t/out-gridview-with-foreach/5046/8 "2015-10-18T10:35:01Z")

</div>

You’re making it way harder than it needs to be.

```
Get-Process 'csrss','svchost','explorer' | 
    select ProcessName,VM | Out-GridView
```

Or have I lost the bubble here because the topic has morphed?

---

<div class="post-metadata">

**Author:** ![mike-eyler](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mike-eyler](https://forums.powershell.org/u/mike-eyler)\
**Post date:** [October 24, 2015, 1:34am UTC](https://forums.powershell.org/t/out-gridview-with-foreach/5046/9 "2015-10-24T01:34:15Z")

</div>

Thanks Curtis!

I figured it out, I needed to specify $Output as an array - @()

$Servers = (‘localhost’,‘Server1’,‘Server2’)  
$Output = @()

foreach($Server in $Servers){  
$Output += Get-WmiObject -Class Win32\_Networkadapter -ComputerName $Server | select PSComputerName,Manufacturer,Description,NetConnectionID,Speed,IPAddress,IPSubnet,DefaultIPGateway,DnsServerSearchOrder,DNSDomainSuffixSearchOrder  
}  
$Output | Out-GridView

---

<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:44pm UTC](https://forums.powershell.org/t/out-gridview-with-foreach/5046/10 "2024-05-16T20:44:33Z")

</div>


