# issues with export-csv or out-gridview.  I'm not understanding the behaviour.

**URL:** <https://forums.powershell.org/t/issues-with-export-csv-or-out-gridview-im-not-understanding-the-behaviour/40>\
**Category:** PowerShell Help\
**Created:** [May 8, 2013, 7:30pm UTC](https://forums.powershell.org/t/issues-with-export-csv-or-out-gridview-im-not-understanding-the-behaviour/40 "2013-05-08T19:30:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![philippe-de-schutter](https://avatars.discourse-cdn.com/v4/letter/p/a587f6/32.png) [@philippe-de-schutter](https://forums.powershell.org/u/philippe-de-schutter)\
**Post date:** [May 8, 2013, 7:30pm UTC](https://forums.powershell.org/t/issues-with-export-csv-or-out-gridview-im-not-understanding-the-behaviour/40/1 "2013-05-08T19:30:35Z")

</div>

First the code:

_$servers = “Server01”, “Server02”_

_ForEach ($server in $servers) {_

_# Get the WMI information_  
 _$os = get-wmiobject -class win32\_operatingsystem -computerName $server_  
 _$cs = get-wmiobject -class win32\_computersystem -computerName $server_  
 _$AssetTag = Get-WmiObject Win32\_SystemEnclosure -ComputerName $server_

_# Create a new Object to contain all necessary WMI information abou the Server_

_$Props = @{ Computername = $os.\_\_Server_  
 _OS = $os.Caption_  
 _BoardManufacturer = $cs.manufacturer_  
 _AssetTag = $AssetTag.SerialNumber_  
 _}_

_$obj = New-Object -TypeName PSObject -Property $Props_

_Â_ _$foo = @()_

_$Foo += $obj  
 $foo  
 }_

Now the question:

the $Foo at the end gives me a table with 2 rows, correctly filled in on the console. Â All seems fine

&nbsp;

Now if I do: Â $Foo | export-csv c:\export.csv Â I only get the information of Server02 in my csv, none of the info of the first server is there…

It doesn’t matter if I do it inside or outside the Foreach loop.

&nbsp;

If I do $Foo | out-gridview Â inside the Foreach loop, it actually opens 2 gridview windows, each containing the information about one of the servers.

&nbsp;

If I do $Foo | out-gridview outside of the Forach loop, then I only get one window that opens, which contains the info of Server02

&nbsp;

&nbsp;

This is driving me crazy, I’m missing something, but I can’t put my hand on it. Â It’s weird that $foo looks fine when I run it in the console (inside the foreach loop) but doesn’t export correctly.

&nbsp;

---

<div class="post-metadata">

**Author:** ![kethrax](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kethrax/32/725_2.png) [@kethrax](https://forums.powershell.org/u/kethrax)\
**Post date:** [May 8, 2013, 8:37pm UTC](https://forums.powershell.org/t/issues-with-export-csv-or-out-gridview-im-not-understanding-the-behaviour/40/2 "2013-05-08T20:37:45Z")

</div>

Hi,

You are doing export to csv into loop “foreach”, try do declare variable ex. _$foo = @() before “foreach” loop Â like this:_

```
$foo = @()
ForEach ($server in $servers) {
...
$foo += something
}
$foo | export-csv .....
```

Should work.

&nbsp;

&nbsp;

Regards,

Kamil Tatar

---

<div class="post-metadata">

**Author:** ![philippe-de-schutter](https://avatars.discourse-cdn.com/v4/letter/p/a587f6/32.png) [@philippe-de-schutter](https://forums.powershell.org/u/philippe-de-schutter)\
**Post date:** [May 8, 2013, 8:55pm UTC](https://forums.powershell.org/t/issues-with-export-csv-or-out-gridview-im-not-understanding-the-behaviour/40/3 "2013-05-08T20:55:56Z")

</div>

Kamil, thank you very much. Â You were spot on.

I assume that IÂ re-declaredÂ $foo () each time the loop ran? (emptying it basically? Â )

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [May 9, 2013, 1:16am UTC](https://forums.powershell.org/t/issues-with-export-csv-or-out-gridview-im-not-understanding-the-behaviour/40/4 "2013-05-09T01:16:51Z")

</div>

And as a note, the _usual_ practice is to just write a function or script that outputs to the pipeline, rather than accumulating the objects in your own array first. Accumulating creates a blocking command in the pipeline.

&nbsp;

---

<div class="post-metadata">

**Author:** ![philippe-de-schutter](https://avatars.discourse-cdn.com/v4/letter/p/a587f6/32.png) [@philippe-de-schutter](https://forums.powershell.org/u/philippe-de-schutter)\
**Post date:** [May 9, 2013, 2:49am UTC](https://forums.powershell.org/t/issues-with-export-csv-or-out-gridview-im-not-understanding-the-behaviour/40/5 "2013-05-09T02:49:53Z")

</div>

I’m afraid that I don’t understand what you mean with that.

I based myself on listing 21.8 of Powershell in Depth, and I don’t see a function in there.

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![jasonmorgan](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jasonmorgan/32/775_2.png) [@jasonmorgan](https://forums.powershell.org/u/jasonmorgan)\
**Post date:** [May 11, 2013, 10:45am UTC](https://forums.powershell.org/t/issues-with-export-csv-or-out-gridview-im-not-understanding-the-behaviour/40/6 "2013-05-11T10:45:18Z")

</div>

Phillippe,

So what Don is trying to tell you is that by creating an object and putting it into an array you’re messing with the flow of the pipeline.Â I’ve modified your script and I’m putting it in a codeblock below:

```
$servers = â€œServer01", "Server02"
ForEach ($server in $servers) 
    {
        # Get the WMI information
        $os = get-wmiobject -class win32_operatingsystem -computerName $server
        $cs = get-wmiobject -class win32_computersystem -computerName $server
        $AssetTag = Get-WmiObject Win32_SystemEnclosure -ComputerName $server

        # Create a new Object to contain all necessary WMI information abou the Server

        $Props = @{ 
                Computername = $os.__Server
                OS = $os.Caption
                BoardManufacturer = $cs.manufacturer
                AssetTag = $AssetTag.SerialNumber
            }
        New-Object -TypeName PSObject -Property $Props
    }
```

Now that script will output an object for each ComputerName you run through it.Â That means you can send it to export-csv or out-gridview or anything else.Â It’s bad practice, and in this case entirely unnecessary, to load custom objects into an array as part of your function or script.

---

<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:57pm UTC](https://forums.powershell.org/t/issues-with-export-csv-or-out-gridview-im-not-understanding-the-behaviour/40/7 "2024-05-16T20:57:54Z")

</div>

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


