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
5,257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Unified and professional email signatures help maintain a consistent company brand image to the outside world. This article shows how to create an email signature in Exchange Server 2010 using a transport rule and how to overcome native limitations …
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
how to add IIS SMTP to handle application/Scanner relays into office 365.

724 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