PowerShell Exported CSV is Garbled?

KB ID 0001557

Problem

I was exporting a list of enabled Active Directory users to a CSV file for some documentation, the finished article was not what I was expecting;

Garbled CSV From PowerShell

Contents;

Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
ClassId2e4f51ef21dd47e99d3c952918aff9cd, 
pageHeaderEntry
pageFooterEntry
autosizeInfo
shapeInfo
groupingEntry
033ecb2bc07a4d43b5ef94ed5a35d280
Microsoft.PowerShell.Commands.Internal.Format.AutosizeInfo
Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo

Solution

This happens if you have used ‘Format-Table‘ in your PowerShell command. ‘Export-Csv’ does not like this, use ‘Select-Object‘ instead. e.g.

DONT USE THIS
Get-ADuser -filter *|where {$_.enabled -eq "True"} | Format-Table Surname, GivenName, Enabled, SamAccountName, UserPrincipalName, Name, DistinguishedName | Export-Csv C:\Output\Enabled-Users.csv
USE THIS
Get-ADuser -filter *|where {$_.enabled -eq "True"} | Select-Object Surname, GivenName, Enabled, SamAccountName, UserPrincipalName, Name, DistinguishedName | Export-Csv C:\Output\Enabled-Users.csv

Related Articles, References, Credits, or External Links

NA

Author: PeteLong

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *