Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-04-12
5
Medium Priority
?
5,777 Views
Last Modified: 2013-06-19
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
}
0
Comment
Question by:TigerBlood
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:soostibi
ID: 35381737
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

0
 

Author Comment

by:TigerBlood
ID: 35384756
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!

0
 
LVL 13

Accepted Solution

by:
soostibi earned 2000 total points
ID: 35387061
You are right, ToLocalTime() makes it perfect.  As far as your first remark is concerned, I do not really know how can that be. You should find this spacific item and see what it really is and maybe that explains it.
In my environment though I also found strange oldest received dates, finally I realized it was an old Task item.
$mbxs = Get-Mailbox 
$mbxs | %{
    $newest = $oldest = $null
    $mb = $_
    $mb | Get-MailboxFolderStatistics -IncludeOldestAndNewestItems | %{
        if($_.NewestItemReceivedDate -and (!$newest -or $newest -lt $_.NewestItemReceivedDate.tolocaltime())){
            $newest = $_.NewestItemReceivedDate.tolocaltime()}
        if($_.OldestItemReceivedDate -and (!$oldest -or $oldest -gt $_.OldestItemReceivedDate.tolocaltime())){
                $oldest = $_.OldestItemReceivedDate.tolocaltime()}
    }
    $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

0
 

Author Closing Comment

by:TigerBlood
ID: 35387296
Fantastic solution.  Very quick response.
0
 

Expert Comment

by:ITMASTER68
ID: 39259674
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
0

Featured Post

Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The main intent of this article is to make you aware of ‘Exchange fail to mount’ error, its effects, causes, and solution.
Stellar Exchange Toolkit: this 5 in 1 toolkit comes loaded with mega-software tool. Here’s an introduction to tools’ usage and advantages:
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question