# Creating a CSV file  from results from for each

**URL:** <https://forums.powershell.org/t/creating-a-csv-file-from-results-from-for-each/6187>\
**Category:** PowerShell Help\
**Created:** [April 11, 2016, 2:58pm UTC](https://forums.powershell.org/t/creating-a-csv-file-from-results-from-for-each/6187 "2016-04-11T14:58:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wei-yen-tan](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wei-yen-tan/32/91_2.png) [@wei-yen-tan](https://forums.powershell.org/u/wei-yen-tan)\
**Post date:** [April 11, 2016, 2:58pm UTC](https://forums.powershell.org/t/creating-a-csv-file-from-results-from-for-each/6187/1 "2016-04-11T14:58:39Z")

</div>

I am writing a bit of code and came across a situation where I have some output from foreach but I cannot figure out how I can export to a csv.

the code i have is as follows:

```
$servers = import-csv D:\server_list.csv

        foreach ($server in $servers){
        $servername = $server.computername        
            try {
            Get-ADComputer $servername -properties description | select name, description -ErrorVariable SilentlyContinue

            } catch{
       
                $testpath = test-path d:\15393848.txt

                    if ($testpath -eq $false){

                        New-item -itemtype file -name 15393848.txt -path d:\
        
                        }

                    Add-Content -Path d:\15393848.txt -value "$servername"

            }
    }
```

It outputs correctly and puts the text in the error log if the computer doesnt exist but need to have the display output in a csv. I had a look around the web and tried to use whats there and i saw one example of one guy use objects ([powershell - Gather info through several foreach and then export-csv at the end of script - Stack Overflow](http://goo.gl/VN7Imr))  
but I don’t think I am understanding the core concept in regards to this.

Any help appreciated.

---

<div class="post-metadata">

**Author:** ![yuan-li](https://avatars.discourse-cdn.com/v4/letter/y/aeb1de/32.png) [@yuan-li](https://forums.powershell.org/u/yuan-li)\
**Post date:** [April 11, 2016, 5:18pm UTC](https://forums.powershell.org/t/creating-a-csv-file-from-results-from-for-each/6187/2 "2016-04-11T17:18:45Z")

</div>

You can define a customized object if you want, but if you just want the result, you can assign a variable to to your for loop. Get-ADComputer has returned an ADObject for you already.

```
$servers = import-csv D:\server_list.csv

     $result= foreach ($server in $servers){
        $servername = $server.computername        
            try {
            Get-ADComputer $servername -properties description | select name, description -ErrorVariable SilentlyContinue

            } catch{
       
                $testpath = test-path d:\15393848.txt

                    if ($testpath -eq $false){

                        New-item -itemtype file -name 15393848.txt -path d:\
        
                        }

                    Add-Content -Path d:\15393848.txt -value "$servername"

            }
    }

$result | export-csv c:\temp\server.csv
```

Also，in your url example, that’s the old way to define a customized object- you define a hash table structure and then convert it to an object. After Powershell v3, you can directly use [pscustomobject] to create one in your loop.

Btw, I think you’re Chinese from your name. If you can read Chinese, you can see my blog for more examples. [Powershell 自定义对象小技巧\_麻婆豆腐的技术博客\_51CTO博客](http://beanxyz.blog.51cto.com/5570417/1737512)

Cheers  
Yuan

---

<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:** [April 11, 2016, 10:30pm UTC](https://forums.powershell.org/t/creating-a-csv-file-from-results-from-for-each/6187/3 "2016-04-11T22:30:57Z")

</div>

You can also change your code a little to use the ForEach-Object Cmdlet rather than the ForEach statement

Example:

```
ForEach ($dir in @("C:\","C:\Users","C:\Windows")){
    Get-ChildItem $dir
}
```

Using the above example, if I added a Pipe to export after the ForEach, I get an error.

```
ForEach ($dir in @("C:\","C:\Users","C:\Windows")){
    Get-ChildItem $dir
} | Export-Csv C:\Temp\Export.csv
```

```
At line:4 char:3
+ } | Export-Csv C:\Temp\Export.csv
+ ~
An empty pipe element is not allowed.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : EmptyPipeElement
```

This is because statements in and of themselves do not export object to the pipeline. In order to get the above code to work, I would need to add the Export on the cmdlet that actually exports the object for Export-CSV to work on.

```
ForEach ($dir in @("C:\","C:\Users","C:\Windows")){
    Get-ChildItem $dir | Export-Csv C:\Temp\Export.csv -Append
}
```

This works OK, but since it is inside the loop, I have to use the -Append parameter so the previous data does not get over-written each time through the loop. That also means additional IO which will cause the script to run slower and use more resources on the system.

What I can do instead is change this to use the ForEach-Object cmdlet to continue the pipeline.

```
@("C:\","C:\Users","C:\Windows") | ForEach-Object {
    Get-ChildItem $_
} | Export-Csv C:\Temp\Export.csv
```

Since ForEach-Object is a cmdlet that outputs objects to the Pipeline, Export-CSV will gladly take those objects as input.

---

<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/creating-a-csv-file-from-results-from-for-each/6187/4 "2024-05-16T20:41:38Z")

</div>


