Script to get mailbox size for users by department

I need assistant getting mailbox size for a group of users that belong to a specific department.  Does anyone know how to use the Department field as a parameter in a script.  Ideally I would like to export a csv of all user mailbox sizes in the forest and have the department field included in the report.  Then I can sort in Excel.  Thanks

Parents
  • Interesting challenge.  The trick here is that neither Get-Mailbox nor Get-MailboxStatistics has the Department field in it.  So we have to fall all the way back to Get-ADUser to get get the Department name.  What this script does is get that information (plus a little more I thought might be useful) and create your CSV for you.  If a user ID is not mail enabled it ignores it and moves on.

    $Results = ForEach ($User in (Get-ADUser -Filter * -Properties Department,Mail))
    {   $Mail = Get-Mailbox $User.Name -ErrorAction SilentlyContinue | Get-MailboxStatistics
        If ($Mail)
        {   New-Object PSObject -Property @{
                Name = $User.Name
                SamAccountName = $User.SamAccountName
                Email = $User.Mail
                Department = $User.Department
                MailboxSize = $Mail.TotalItemSize.Value.ToBytes()
            }
        }
    }
    $Results | Select Name,SamAccountName,Email,Department,MailboxSize | Export-Csv c:\Scripts\MailboxSizeByDepartment.csv -NoTypeInformation

  • I get the following error when I try to run your script.  Any ideas.  Thank you so much for the help

     

    The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
    spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\Downloads\Scripts\Mailbox Size per department.ps1:1 char:41
    + $Results = ForEach ($User in (Get-ADUser <<<<  -Filter * -Properties Department,Mail))
        + CategoryInfo          : ObjectNotFound: (Get-ADUser:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

  • OK, last question I will ask you - I promise.  Now my boss has decided he only wants the result from 1 domain.  This is the domain that the Exchange servers are a member of, and really the only domain that contains users. How do I make the script only query the domain that the exchange server is a member of.  In other words, I do not need the entire forest? Thanks a billion

  • LOL, ok, we'll take out the loop for the Forest:

    cls
    Function Get-Quota
    {   Param (
            [string]$Quota,
            [string]$DB
        )
        
        If ($Quota -eq "unlimited")
        {   Return $Database[$DB]
        }
        Else
        {   Return $Quota
        }
    }
    #Load ActiveDirectory module
    Try { Import-Module ActiveDirectory -ErrorAction Stop }
    Catch { Write-Host "Unable to load Active Directory module, is RSAT installed?"; Exit }
    #Load Exchange tools
    If (-not (Get-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue))
    {   Try { Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction Stop }
        Catch { Write-Host "Unable to load Exchange tools because ""$($Error[0])""" -ForegroundColor Red ; Exit }
    }

    $Database = @{}
    ForEach ($DB in (Get-MailboxDatabase))
    {   $Database.Add($DB.Name,(New-Object PSObject -Property @{
            IssueWarningQuota = $DB.IssueWarningQuota
            ProhibitSendQuota = $DB.ProhibitSendQuota
            ProhibitSendReceiveQuota = $DB.ProhibitSendReceiveQuota
        }))
    }
    $Results = @()
    $Results += ForEach ($User in (Get-ADUser -Filter * -Properties Department,Mail))
    {   $Mailbox = Get-Mailbox $User.Name -ErrorAction SilentlyContinue
        If ($Mailbox)
        {   $Mail = $Mailbox | Get-MailboxStatistics -ErrorAction SilentlyContinue
            If ($Mail.TotalItemSize.Value -eq $null)
            {   $TotalSize = 0
            }
            Else
            {   $TotalSize = $Mail.TotalItemSize.Value.ToBytes()
            }
            New-Object PSObject -Property @{
                Name = $User.Name
                SamAccountName = $User.SamAccountName
                Email = $User.Mail
                Department = $User.Department
                MailboxSize = $TotalSize
                IssueWarningQuota = (Get-Quota $Mailbox.IssueWarningQuota $Mailbox.Database).IssueWarningQuota
                ProhibitSendQuota = (Get-Quota $Mailbox.ProhibitSendQuota $Mailbox.Database).ProhibitSendQuota
                ProhibitSendReceiveQuota = (Get-Quota $Mailbox.ProhibitSendReceiveQuota $Mailbox.Database).ProhibitSendReceiveQuota
            }
        }
    }
    $Results | Select Name,SamAccountName,Email,Department,MailboxSize,IssueWarningQuota,ProhibitSendQuota,ProhibitSendReceiveQuota | Export-Csv c:\Scripts\MailboxSizeByDepartment.csv -NoTypeInformation

  • I love bosses.  They ask for the world without any clue as to what it takes to get it done

  • Most of my users in the Org are set to use Mailbox database quota defaults. However, we have some one-offs that have been manually configured to have a 2500 MB Prohibit Send Value assigned. How do I get the script to pull from the user's recipient configuration, instead of puliing from the default setting for the database?  Thanks

  • Will this script work for M365, mailboxes stored in cloud?  we are hybrid environment-  but this report with users quota status along with dept attribute is exactly what I need.  thank you

Reply Children
No Data