Link to home
Start Free TrialLog in
Avatar of TigerBlood
TigerBlood

asked on

Can Powershell run a script to output the overall oldest item received/ overall newest item received in an Exchange 2010 mailbox?

I am trying to create a powershell script in Exchange 2010 that outputs the overall oldest item received and overall newest item received for each individual mailbox.  In addition, the script should output the username, the number of items total in the entire mailbox, and the retention policy that is set for that mailbox.

I have the script below, which outputs the oldest/ newest items received but it outputs for each folder and subfolder (i.e. inbox, sent items, any manually created mail folders, etc.).  Sure, I could compare manually the oldest/ newest items received for each folder but this would be very time consuming as we have a large amount of mailboxes and I am wondering if this could be done in powershell.  the output should be to a CSV file.

Thanks in advance for your help!
 
$temp="C:\mbs_temp.csv"
$outputfile="C:\mbs_output.csv"

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
}
Avatar of soostibi
soostibi
Flag of Hungary image

Try this:
$mbxs = Get-Mailbox 
$mbxs | %{
    $newest = $oldest = $null
    $mb = $_
    $mb | Get-MailboxFolderStatistics -IncludeOldestAndNewestItems | %{
        if($_.NewestItemReceivedDate -and (!$newest -or $newest -lt $_.NewestItemReceivedDate)){$newest = $_.NewestItemReceivedDate}
        if($_.OldestItemReceivedDate -and (!$oldest -or $oldest -gt $_.OldestItemReceivedDate)){$oldest = $_.OldestItemReceivedDate}
    }
    $stat = $mb | Get-MailboxStatistics
    New-Object -TypeName psobject -Property @{
        DisplayName = $mb.displayname
        SMTPAddress = $mb.PrimarySMTPAddress.tostring()
        TotalItems = $stat.itemcount
        RetentionPolicy = $mb.retentionpolicy
        NewestItem = $newest
        OldestItem = $oldest        
    }
} | Select-Object -Property DisplayName, SMTPAddress, TotalItems, RetentionPolicy, NewestItem, OldestItem |
    Export-Csv C:\AdminScripts\yourpathandfile.csv -NoTypeInformation

Open in new window

Avatar of TigerBlood
TigerBlood

ASKER

Very nice script!  this pulled the info and exported to CSV.  However, I have only been at this firm since Oct 2010, but it tells me the oldest item I have is in May 2010.  How is this possible?  

Also, the newest items seem to be off by 4hrs.  They report a time that is 4hrs into the future.  I researched this a little and ran the get-date -f zz in my powerShell and it returned -04, which I understand to be my timezone offset.  I see there is a ToLocalTime command to address this specific issue and I have tried to add this to your script but I am receiving all kinds of errors.  Ah... powershell scripting is really not my strong point! :)  Would you know how to add this?  Thanks!

ASKER CERTIFIED SOLUTION
Avatar of soostibi
soostibi
Flag of Hungary image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Fantastic solution.  Very quick response.
Im wondering how you can tweak this script to also include the online archive mailbox and the mailbox in this query, perhaps a -archive somewhere, but not sure where, i'd want the results to include both. Thanks