# Exporting Table to CSV

**URL:** <https://forums.powershell.org/t/exporting-table-to-csv/17929>\
**Category:** PowerShell Help\
**Created:** [November 11, 2021, 5:39pm UTC](https://forums.powershell.org/t/exporting-table-to-csv/17929 "2021-11-11T17:39:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fuzbuster83](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@fuzbuster83](https://forums.powershell.org/u/fuzbuster83)\
**Post date:** [November 11, 2021, 5:39pm UTC](https://forums.powershell.org/t/exporting-table-to-csv/17929/1 "2021-11-11T17:39:20Z")

</div>

I have found and modified this snippet to output exactly what I need inside of a Powershell window:

Get-ADOrganizationalUnit -Properties canonicalname -Filter \*|  
ForEach-Object{  
[pscustomobject]@{  
OUName = Split-Path $_.CanonicalName -Leaf  
CanonicalName = $_.CanonicalName  
UserCount = (Get-AdUser -Filter \* -SearchBase $_.DistinguishedName -SearchScope OneLevel).Count  
ComputerCount = (Get-AdComputer -Filter \* -SearchBase $_.DistinguishedName -SearchScope OneLevel).Count  
}  
}  
It displays a simple table with the 4 columns I need, but I’m struggling with where I can input the command to export this output into a CSV. When I do get it to create a file, the .csv is empty.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 11, 2021, 7:41pm UTC](https://forums.powershell.org/t/exporting-table-to-csv/17929/2 "2021-11-11T19:41:36Z")

</div>

> [@fuzbuster83](#):
>
> output into a CSV

If you want to export some structured data to a CSV file you can use

> **[Export-Csv (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-7.2)**
>
> The Export-CSV cmdlet creates a CSV file of the objects that you submit. Each object is a row that includes a character-separated list of the object's property values. You can use the Export-CSV cmdlet to create spreadsheets and share data with...

Please read the help completely including the examples to learn how to use it.

You may read as well the help for

> **[Get-Command (Microsoft.PowerShell.Core) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/get-command?view=powershell-7.2)**
>
> The Get-Command cmdlet gets all commands that are installed on the computer, including cmdlets, aliases, functions, filters, scripts, and applications. Get-Command gets the commands from PowerShell modules and commands that were imported from other...

This could help you to find some cmdlets you don’t know about yet. 😉

Regardless of that when you post code please format it as code using the _preformatted text_ button ( **\</\>** ), simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![fuzbuster83](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@fuzbuster83](https://forums.powershell.org/u/fuzbuster83)\
**Post date:** [November 11, 2021, 7:46pm UTC](https://forums.powershell.org/t/exporting-table-to-csv/17929/3 "2021-11-11T19:46:28Z")

</div>

Thank you!

I will check out the links this afternoon.

---

<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:20pm UTC](https://forums.powershell.org/t/exporting-table-to-csv/17929/4 "2024-05-16T20:20:26Z")

</div>


