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

1 Comment

  1. Ok.. but now instead of gibberish, I only see “length 76”.

    I’m trying to do something like this:
    $message = “This is a table of users without email_field” | Export-CSV -path $myFile -Append -NoTypeInformation -force
    $myTableThatPrintsFineInPowershell | Export-CSV -path $myFile -Append -NoTypeInformation -force
    $message = “This is another table of users with email_field” | Export-CSV -path $myFile -Append -NoTypeInformation -force
    $myTableThatPrintsFineInPowershell | Export-CSV -path $myFile -Append -NoTypeInformation -force

    The contents of the variables doesn’t matter really, but what I’m trying to do is print a message, then print a table, both saved in different variables.

    If I run them separately in the console, I can either get $myTable printed properly, or just Lenght 76 (which also is the only thing that shows up if try to export both variables at the same time). If I export with out-file, then I can print the message and the table together, but the table looses the table-format. If I export the message as out-file and the table as csv, I only get the message exorted and nothing more.. but the table on its own gets exported fine

    Post a Reply

Submit a Comment

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