The script returns the cumulative size of the requested folder with and without subfolders counted in.
<# ################################################# # Function to display folder sizes in a mailbox # ################################################# Syntax: FolderSize -Mailbox Alice -Folder Inbox -MB or with pipeline input Get-Mailbox Alice | FolderSize -Folder Inbox or querying all folders in KB measurements Get-Mailbox Alice | FolderSize -KB #> function FolderSize { param ( [Parameter(Mandatory=$true, ValueFromPipeline=$true)] [string]$Mailbox, [Parameter(Mandatory=$false)] [string]$Folder, [Parameter(Mandatory=$false)] [switch]$MB, [Parameter(Mandatory=$false)] [switch]$KB ) # Check if mailbox exists if ((Get-Mailbox $Mailbox -ErrorAction silent) -eq $null) { Write-Host -f magenta "$Mailbox does not exist or mistyped."; break} # If Folder is not specified, query all if ($Folder -like "") { $Folder = "*" } # Getting folder list and sizes Get-MailboxFolderStatistics $Mailbox | ? name -like $Folder | %{ $name = $_.name $size = $_.FolderSize.split("( ")[3].replace(",","") $all = $_.FolderAndSubfolderSize.split("( ")[3].replace(",","") # GB, MB or KB if ($MB) { $size = '{0:N3}' -f ($size / [math]::pow(1024,2)) $all = '{0:N3}' -f ($all / [math]::pow(1024,2)) $ms = 'MB' } elseif ($KB) { $size = '{0:N3}' -f ($size / [math]::pow(1024,1)) $all = '{0:N3}' -f ($all / [math]::pow(1024,1)) $ms = 'KB' } else { $size = '{0:N3}' -f ($size / [math]::pow(1024,3)) $all = '{0:N3}' -f ($all / [math]::pow(1024,3)) $ms = 'GB' } # Display the results [pscustomobject]@{"Mailbox" = $Mailbox; "Folder" = $name; "Size ($ms)" = $size; "Size With Subfolders ($ms)" = $all} } }
# Example PS C:\> get-mailbox alice | FolderSize -Folder inbox -MB Mailbox Folder Size (MB) Size With Subfolders (MB) ------- ------ --------- ------------------------- Alice Inbox 3.284 7.324
Comments