Link to home
Start Free TrialLog in
Avatar of TigerBlood
TigerBlood

asked on

How do I output Exchange mailboxStatistics to CSV file?

I have the following script, which generates a report on exchange mailboxes and I want it to output values for all the fields listed in the "Select-Object" section at the end of the script.  Only the DisplayName, SMTP Address, Retention Policy, NewestItem, and OldesItem are being output.  I am trying to get it to output a value for Total Items, NewestItemFolder and OldestItemFolder.  What changes do I need to make to my script for this to work?

Also, with the script in it's current state are other folders/ sub-folders in the mailbox that are created by the user with the intent for holding emails scanned? If now, how would I add that to the script?

The script should only scan the Inbox, Sent Items and any user created folder used for storing emails.  I do not wish to scan Calendars, Tasks, Notes or Contacts in my script.  

 
$mbxs = Get-Mailbox 
$typestoinclude = "Inbox", “SentItems”
$recurse = $true

filter get-folders ($roottypes, [switch] $recurse){
$mb = $_
$roottypes | %{
$type = $_
$root = $mb | Get-MailboxFolderStatistics -FolderScope $type -IncludeOldestAndNewestItems |?{$_.foldertype -eq $type} 
if($recurse){
    $mb | get-mailboxfolderstatistics -IncludeOldestAndNewestItems |  ?{$_.folderpath -match "^$($root.folderpath)"} 
}
else {$root}
}
}

$mbxs | %{
    $newest = $oldest = $null
    $mb = $_
    $mb | Get-folders $typestoinclude -recurse:$recurse | %{
        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()  
            RetentionPolicy = $mb.retentionpolicy  
            ItemsInMailbox = $stat.ItemCount
            NewestItem = $newest
            NewestItemFolder = $newestrow.name
            OldestItem = $oldest
            OldestItemFolder = $oldestrow.name
        }
} | Select-Object -Property displayName, SMTPAddress, RetentionPolicy, TotalItems, NewestItem, NewestItemFolder, OldestItem, OldestItemFolder | Export-Csv C:\output.csv –NoTypeInformation

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of endital1097
endital1097
Flag of United States of America 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
Avatar of TigerBlood
TigerBlood

ASKER

Thanks for your response, Endital1097.  Just looking into this.