Tilt-logo

Things I Learned Today - our daily eureka-moments

Carr
proppi
Edland
feenyx
Programming .NET VS2010 Web Windows VS2012 S3 Search SQL SqlMetal Accessibility Amazon Android App BBQ EBS EC2 Exchange Food Garmin Geocaching GPS Grill Java Linq Lucene MVC PowerShell

Piping Format-Table to Export-Csv (PowerShell) - Edland, 19.08.2011

When working with PowerShell to export information from Exchange, I ended up with unreadable data in my csv-file when i tried to export sync-times for mobile devices.

Get-Mailbox -OrganizationalUnit Accounting | ForEach {Get-ActiveSyncDeviceStatistics -Mailbox:$_.Identity | Format-Table Identity,LastSuccessSync | Export-Csv filename.csv 


This will give you output like this:


#TYPE Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
ClassId2e4f51ef21dd47e99d3c952918aff9cd,"pageHeaderEntry","pageFooterEntry","autosizeInfo","shapeInfo","groupingEntry"
033ecb2bc17a4d43c5ef94ed5a35d272,,,,"Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo",
be211fe47d09416682f34cb69c38b8c1,,,,,

Not very helpful..
This is because the default Output for PowerShell will format this for you, but Export-Csv will not.
This is fixed by replacing Format-Table with the Select-Object cmdlet like this:

Get-Mailbox -OrganizationalUnit Accounting | ForEach {Get-ActiveSyncDeviceStatistics -Mailbox:$_.Identity | Select-Object Identity,LastSuccessSync | Export-Csv filename.csv 


That will give you the output you expected
Tags: Exchange PowerShell
Comments:
Nobody has commented on this post yet. Feel free to be the first!