Search This Blog

Monday, April 5, 2010

Format-Table Export-Csv

If you want to export the output of your powershell command to a csv file, you cannot use format command to pipe the output of the command. If you try to use the format command you might get a strange looking output file.
Example:
If you would use the following command:
[Code]
Get-mailbox | get-user | ft 'name','Lastname','Samaccountname' | export-csv -path c:\temp\users.csv
[/code]
output:
#TYPE Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
ClassId2e4f51ef21dd47e99d3c952918aff9cd,"pageHeaderEntry","pageFooterEntry","autosizeInfo","shapeInfo","groupingEntry"
033ecb2bc07a4d43b5ef94ed5a35d280,,,,"Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo",
9e210fe47d09416682b841769c78b8a3,,,,,
27c87ef9bbda4f709f6b4002fa4af63c,,,,,
27c87ef9bbda4f709f6b4002fa4af63c,,,,,
27c87ef9bbda4f709f6b4002fa4af63c,,,,,
27c87ef9bbda4f709f6b4002fa4af63c,,,,,
27c87ef9bbda4f709f6b4002fa4af63c,,,,,
27c87ef9bbda4f709f6b4002fa4af63c,,,,,
..............
The correct way to get a proper CSV is to use the select-object cmdlet.
[Code]
Get-mailbox | get-user | select-object 'name','Lastname','Samaccountname' | export-csv -path c:\temp\users.csv
[/code]
output:
#TYPE Selected.Microsoft.Exchange.Data.Directory.Management.User
Name,"LastName","SamAccountName"
Alain,"De Backer","adebac"
Anny,"De Vriendt","adevri"
Eilish,"Bosuyt","Ebosuy"
Thais,"Geeraerts","Tgeera"
............

No comments:

Post a Comment