Exporting Powershell to formatted CSV file


Running Powershell v2. I have the following command that pulls mailbox name and sizes for a list of users.

But when it exports to CSV, the data is not formatted properly. What I'd like to do is have Display Name and Mailbox Size in seperate columns on the CSV file but this doesn't seem possible? When the data is exported it all appears in the same column?

Get-content c:\LIST.txt | Get-mailbox | Foreach-object {get-mailboxstatistics -identity $_} | fl displayname, @{ expression={$_.TotalItemSize.Value.ToMB()}} > c:\export.csv

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hendrik WieseInformation Security ManagerCommented:
Have you tried it like this:

Get-content c:\LIST.txt | Get-mailbox | Foreach-object {get-mailboxstatistics -identity $_} | ft displayname, @{ expression={$_.TotalItemSize.Value.ToMB()}} > c:\export.csv

Open in new window

You can use the export-csv cmdlets like this

Foreach ($User in (get-content c:\list.txt)){
    Get-mailbox $User | get-mailboxstatistics | Select displayname, @{n = 'TotalSize'; e={$_.TotalItemSize.Value.ToMB()}} | export-csv c:\temp\export.csv -notype}

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Don't use any formatting cmdlet (format-*) if you export into CSV. Just use export-csv - it is exactly for that purpose:
Get-content c:\LIST.txt | Get-mailbox | Foreach-object {get-mailboxstatistics -identity $_}  | select displayname, @{n="Size (MB)"; e={$_.TotalItemSize.Value.ToMB()}} | export-csv c:\export.csv

Open in new window

bruce_77Author Commented:

So what is the difference between using "| fl displayname" and "| select displayname"?
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
fl = format-list: formatting will be   Property: Value   with the properties displayed on separate lines. The result is a stream of text lines, and there are no properties anymore. The same applies to all formatting cmdlets (format-*), as they all result in a stream of text lines.
format-list is usually only applied to console output. format-table can be used for text files, too, but only if you do not need further processing.

select = select-object: Creates new objects containing only those properties you selected (plus the default Object class properties, like name, type, aso.). That is, the result is a collection of objects again, and hence can be processed further. In particular, property names, types and data length are retained. So if put into export-csv, the cmdlet knows where to put delimiters.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.