# Write-Output to file ForEach loop

**URL:** <https://forums.powershell.org/t/write-output-to-file-foreach-loop/598>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/write-output-to-file-foreach-loop/598 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/write-output-to-file-foreach-loop/598/1 "2011-12-31T18:30:00Z")

</div>

by netman06 at 2013-02-13 10:11:13

> Hello,  
>   
> I have never needed to have a ForEach loop, output it’s data other then a one liner to a file.  
>   
> So how can I perform it using this code. Since I would like to use either the Export-Output or maybe Convert-To-HTML, but need to figure out how to perform the output first.  
>   
> `ForEach ($Site in $ADSites) { ## OPEN ForEach Site in ADSites $SiteName = $Site.Name $SiteSubnets = $Site.Subnets $SiteServers = $Site.Servers $SiteAdjacentSites = $Site.AdjacentSites $SiteSiteLinks = $Site.SiteLinks $SiteInterSiteTopologyGenerator = $Site.InterSiteTopologyGenerator $SiteOptions = $Site.Options Write-Output â€œSite Name: $SiteName râ€<br> Write-Output â€œSite Subnets&#58; $SiteSubnets râ€ Write-Output â€œSite Servers: $SiteServers râ€<br> Write-Output â€œAdjacent Sites&#58; $SiteAdjacentSites râ€ Write-Output â€œSite Links: $SiteSiteLinks râ€<br> Write-Output â€œSite InterSiteTopologyGenerator&#58; $SiteInterSiteTopologyGenerator râ€ Write-Output â€œSite Options: $SiteOptions râ€<br> Write-Output &quot; r" | Out-File "C:\My Scripts\Results.txt"`  
>   
> This is the line that is casing me troubles.  
>   
> Write-Output " `r" | Out-File "C:\My Scripts\Results.txt"  
>   
> I do not know how to make it output to a file.  
>   
> Any Ideas would be great.  
>   
> Thanks, Mike

by DonJ at 2013-02-13 10:32:58

> You need to create a custom object and output that. Right now you’re outputting formatted text - bad.  
>   
> `$data = @{‘Site Name’=$sitename;‘Site Subnets’=$SiteSubnets;‘Site Servers’=$SiteServers}$obj = New-Object -Type PSObject -Prop $dataWrite-Output $obj`  
>   
> Something like that. You can add as many pieces of data to that hash table as you need - I added three as an example. Let’s assume this is all in a script called MyScript.ps1.  
>   
> `.\MyScript.ps1 | ConvertTo-HTML | Out-File whatever.html`  
>   
> Is how you convert the output. Don’t put Out-File into the script itself, or you’ll eliminate the possibility of converting it properly.

by netman06 at 2013-02-13 13:17:59

> Hi Don,  
>   
> So this is how I set it up:  
>   
> `ForEach ($Site in $ADSites) { ## OPEN ForEach Site in ADSites $SiteName = $Site.Name $SiteSubnets = $Site.Subnets $SiteServers = $Site.Servers $SiteAdjacentSites = $Site.AdjacentSites $SiteSiteLinks = $Site.SiteLinks $SiteInterSiteTopologyGenerator = $Site.InterSiteTopologyGenerator $SiteOptions = $Site.Options $data = @{‘Site Name’=$sitename;‘Site Subnets’=$SiteSubnets;‘Site Servers’=$SiteServers;‘$SiteAdjacentSites’=$Site.AdjacentSites;‘$SiteSiteLinks’=$Site.SiteLinks;‘$SiteInterSiteTopologyGenerator’=$Site.InterSiteTopologyGenerator;‘$SiteOptions’=$Site.Options} $obj = New-Object -Type PSObject -Prop $data Write-Output $obj`  
>   
> But it seems to not output to file in the loop, can you see any problem in my ForEach statement?  
>   
> Thanks,

by DonJ at 2013-02-13 13:27:17

> It won’t output to the file. You need to do what I told you above: PIPE the results of the script to another command, like ConvertTo-HTML or Out-File. The script is just producing the data (objects) - you can then PIPE those to another command to export them, convert them, etc.  
>   
> If you want this all in one script:  
>   
> `function DoSites {ForEach ($Site in $ADSites) { ## OPEN ForEach Site in ADSites $SiteName = $Site.Name $SiteSubnets = $Site.Subnets $SiteServers = $Site.Servers $SiteAdjacentSites = $Site.AdjacentSites $SiteSiteLinks = $Site.SiteLinks $SiteInterSiteTopologyGenerator = $Site.InterSiteTopologyGenerator $SiteOptions = $Site.Options $data = @{‘Site Name’=$sitename;‘Site Subnets’=$SiteSubnets;‘Site Servers’=$SiteServers;‘$SiteAdjacentSites’=$Site.AdjacentSites;‘$SiteSiteLinks’=$Site.SiteLinks;‘$SiteInterSiteTopologyGenerator’=$Site.InterSiteTopologyGenerator;‘$SiteOptions’=$Site.Options} $obj = New-Object -Type PSObject -Prop $data Write-Output $obj}DoSites | Out-File whatever.txt`  
>   
> Write-Output puts objects in the PowerShell pipeline… from there, other commands can do whatever they want with them. Consider looking into my "Toolmaking" book ([powershellbooks.com](http://powershellbooks.com)). It covers this whole approach and the technique… it’s the ‘right’ way to be building these things in PowerShell.

by netman06 at 2013-02-13 14:54:28

> Hi Don,  
>   
> Yes, that works now. I did purchase your book, I have not yet read it, so this will make me.  
>   
> One last question, is where is the out-file sorting happening, since your this new method, it seems that my data is not in the correct order, like when I was using the Write-Output method.  
>   
> I would think that this is performed at the $data - @{'SiteName, etc level. I tried a Sort-Object before the Out-File whatever.txt, but that did not work either.  
>   
> Thanks,

---

<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, 9:02pm UTC](https://forums.powershell.org/t/write-output-to-file-foreach-loop/598/2 "2024-05-16T21:02:45Z")

</div>


