how do I get this Exchange 2010 PowerShell Script to print to a CSV file?

I have the following powershell script, which prints to the powershell prompt.  How can I get this to print to a csv file?

Thanks

foreach($mbx in Get-Mailbox){
Get-MailboxFolderStatistics -includeoldestandnewestitems $mbx.identity | select @{n="DisplayName";e={$mbx.displayName}},FolderPath,ItemsInFolder,FolderSize,OldestItemreceivedDate,NewestItemreceivedDate
}
TigerBloodAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

waleedaCommented:
you can put at the end  | export-csv c:\results.csv

0
MegaNuk3Commented:
Get-Mailbox | Get-MailboxFolderStatistics -includeoldestandnewestitems | select DisplayName,FolderPath,ItemsInFolder,FolderSize,OldestItemreceivedDate,NewestItemreceivedDate | export-CSV c:\myresults.csv
0
prashanthdCommented:
export-csv has a limitation and cannot be appended, so will store only one MBX info.

Try the following...
$temp="C:\mbs_temp.txt"
$outputfile="C:\mbs_output.txt"

foreach($mbx in Get-Mailbox){
Get-MailboxFolderStatistics -includeoldestandnewestitems $mbx.identity | select @{n="DisplayName";e={$mbx.displayName}},FolderPath,ItemsInFolder,FolderSize,OldestItemreceivedDate,NewestItemreceivedDate | Export-Csv $temp
$readtemp=get-Content $temp
$readtemp | Out-File $outputfile -Append -NoClobber
Remove-Item $temp
}

Open in new window

0

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
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

MegaNuk3Commented:
@prashanthd - that's why I posted my version of the script ;-)
0
waleedaCommented:
@ MegaNuk, you did not add any new thing than i have added before you :-)
0
prashanthdCommented:
@MegaNuk3 :-)
0
prashanthdCommented:
@MegaNuk3 has got it right...
0
Chris DentPowerShell DeveloperCommented:

> @ MegaNuk, you did not add any new thing than i have added before you :-)

Clarity.

Adding "| Export-Csv ... " to the original example would not work because a foreach () { } loop is being used. Since that loop has no output pipeline your suggestion is incomplete at best.

MegaNuk3's suggestion is the most complete and will address the problem at hand.

Chris
0
TigerBloodAuthor Commented:
Awesome script, Prashanthd!
0
TigerBloodAuthor Commented:
thanks all for your super fast replies.  All good and very helpful.

@ MegaNuk - I ran your script but no values get created for the DisplayName.

@ Prashanthd, your script was very good.  The only change I made was to create csv files as opposed to txt files, which was an easy fix.  however when i open the file in csv all the data per row is in one cell.  Is there a way for each data value that is produced goes in seperate cells?

0
Chris DentPowerShell DeveloperCommented:
You could do this.
Get-Mailbox | ForEach-Object {
  $DisplayName = $_.DisplayName
  $_ | Get-MailboxFolderStatistics -IncludeOldestAndNewestItems |
    Select-Object @{n='DisplayName';e={ $DisplayName }}, FolderPath, ItemsInFolder, FolderSize, OldestItemreceivedDate, NewestItemreceivedDate
} | Export-Csv "Output.csv" -NoTypeInformation

Open in new window

It's most like your own, we just switch to ForEach-Object which does have an output pipeline and does let us stick Export-Csv on the end.

Chris
0
TigerBloodAuthor Commented:
Hey Chris - I get an error when I run your script that "pipeline cannot be run concurrently"
0
MegaNuk3Commented:
Yep, you are right. No Display Name from mine.
0
Chris DentPowerShell DeveloperCommented:
Hmm I bet you're using PowerShell 1? Exchange 2007?

Lets see if it prefers this one.
Get-Mailbox | ForEach-Object {
  $DisplayName = $_.DisplayName
  Get-MailboxFolderStatistics $_.Identity -IncludeOldestAndNewestItems |
    Select-Object @{n='DisplayName';e={ $DisplayName }}, FolderPath, ItemsInFolder, FolderSize, OldestItemreceivedDate, NewestItemreceivedDate
} | Export-Csv "Output.csv" -NoTypeInformation

Open in new window

Chris
0
prashanthdCommented:

Hi TigerBlood,

Could you try importing the .csv file in excel, instead of opening

regards
Prashanth
0
TigerBloodAuthor Commented:
@Meganuk3 - Besides the display name not showing up your script puts each value into its own cell, as it should.

If you could get the display name to show up too your script would be fantastic!
0
TigerBloodAuthor Commented:
@Chris-Dent - same error occurs.

@ Prashanthd - hey, that did the trick!  Thanks, my issue is fully resolved!  
0
MegaNuk3Commented:
You could use Identity instead of displayName in mine, but that would give you the mailbox name and folder it is getting statistics for.
0
TigerBloodAuthor Commented:
One last problem with this solution, which just occurred to me.  The output shows each individual folder within the mailbox and the statistics for it.  Is there a way to output only the overall oldest item date and overall newest item date per mailbox.

It might need something like a loop to compare the oldest/ newest item in each folder to get to the oldest/ newest item in the mailbox.  At the end of it, it should have the oldest/ newest item for each mailbox.
0
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
Powershell

From novice to tech pro — start learning today.