Skip to content

PowerShell: Pipe output to CSV file

Last week, I showed you how to get specific properties for all users in a given OU. Knowing that the output of that query quickly gets hard to navigate, wanting to remove the extraneous data that the customer didn’t request, and assuming that they wanted to manipulate the data, I decided to return the results as a CSV-file.

Naturally, PowerShell has an applet for this, We simply take the command we bult last week, and pipe it into that applet. In computing terms, a pipe denotes this character: |.

The applet is called Export-CSV. It requires that you specify the location and name of the output file. As a result, the query looked like this:

Get-ADUser -Filter * -SearchBase "OU=Finance,OU=UserAccounts,DC=FABRIKAM,DC=COM" -properties CN, DisplayName, LastLogonDate, Manager | Export-CSV -path .\OUName.csv

Because my customers operate in Norway, there are the occasional names with the three letters of the Norwegian alphabet – Æ, Ø, and Å. Normally, these would be output as “?”. To avoid this, I tacked on the Encoding parameter to ensure that the output was encoded in Unicode. This resulted in the following query:

Get-ADUser -Filter * -SearchBase "OU=Finance,OU=UserAccounts,DC=FABRIKAM,DC=COM" -properties CN, DisplayName, LastLogonDate, Manager | Export-CSV -path .\OUName.csv -Encoding Unicode

Sometimes, we want to run the same query on multiple users or groups. To achieve this, we use the Append parameter, like so:

Get-ADUser -Filter * -SearchBase "OU=Finance,OU=UserAccounts,DC=FABRIKAM,DC=COM" -properties CN, DisplayName, LastLogonDate, Manager | Export-CSV -path .\OUName.csv -Encoding Unicode -Append

Be First to Comment

By posting a comment, you consent to our collecting the information you enter. See privacy policy for more information.

This site uses Akismet to reduce spam. Learn how your comment data is processed.