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?
 
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
 
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
WEBINAR: 10 Easy Ways to Lose a Password

Join us on June 27th at 8 am PDT to learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees. We'll cover the importance of multi-factor authentication and how these solutions can better protect your business!

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

All Courses

From novice to tech pro — start learning today.