0

I'm using the following code to get a report of our exchange mailboxes sizes and usage the output of this script shows on CSV file this columns:

DisplayName, FreeSpace, TotalItemSize, alias, IssueWarningQuota, ProhibitsendReceiveQuota ProhibitSendQuota

my questions are:

  1. all these columns except for the FreeSpace shows the size in bytes for example: 2.784 GB (2,988,883,006 bytes) i want these columns: TotalItemSize, IssueWarningQuota, ProhibitsendReceiveQuota, ProhibitSendQuota to show only the number without GB and the full bytes size like the FreeSpace column

  2. add new column FreeSpace % that will calculate the free space for the mailbox.

Thanks !

# Load Exchange Management Shell in PowerShell ISE
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn

# Delete csv file on "C:\script"

Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox | Get-MailboxStatistics |
Select DisplayName, @{n="Total Size (MB)";e={[math]::Round(($_.TotalItemSize.ToString().Split("(")[1].Split(" ")[0].Replace(",","")/1MB),2)}}, StorageLimitStatus


$Result=@() 
#Get all user mailboxes
$mailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox
 
#Get all shared mailboxes
#$mailboxes = Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails SharedMailbox
 
$totalmbx = $mailboxes.Count
$i = 0 
$mailboxes | ForEach-Object {
$mbx = $_
#Get mailbox statistics 
$mbs = Get-MailboxStatistics -Identity $mbx.Identity
 
$i++
Write-Progress -activity "Processing $mbx" -status "$i out of $totalmbx completed"
 
if ($mbs.TotalItemSize -ne $null){
$size = [math]::Round(($mbs.TotalItemSize.ToString().Split('(')[1].Split(' ')[0].Replace(',','')/1MB),2)
}else{
$size = 0 }
 
$Result += New-Object -TypeName PSObject -Property $([ordered]@{
Name = $mbx.DisplayName
PrimarySmtpAddress = $mbx.PrimarySmtpAddress
AliasSmtpAddresses = ($mbx.EmailAddresses | Where-Object {$_ -clike 'smtp:*'} | ForEach-Object {$_ -replace 'smtp:',''}) -join ';'
TotalSizeInMB = $size
SizeWarningQuota=$mbx.IssueWarningQuota
StorageSizeLimit = $mbx.ProhibitSendQuota
StorageLimitStatus = $mbs.ProhibitSendQuota
})
}
$Result | Export-CSV "C:\Temp\MailboxSizeReport.csv" -NoTypeInformation -Encoding UTF8








$results = ForEach($mb in $mailboxes){
    $stats=get-mailboxstatistics $mb
    if ($mb.ProhibitSendQuota -eq 'Unlimited') {
        $freespace = 'Unlimited'
    } 
    else {
        $totalBytes = [double]($stats.totalitemsize -replace '.*?\((.*?) bytes.*','$1')
        $prohibitBytes = [double]($mb.ProhibitSendQuota -replace '.*?\((.*?) bytes.*','$1')
        $freespace = [Math]::Round(($prohibitBytes - $totalbytes)/1GB,2)
    }
    $props=@{
        alias=$mb.alias
        DisplayName=$mb.displayname
        #StorageLimitStatus=$stats.StorageLimitStatus
        TotalItemSize=$stats.totalitemsize
        #DatabaseName=$stats.databasename
        ProhibitSendQuota=$mb.ProhibitSendQuota
        ProhibitsendReceiveQuota=$mb.ProhibitsendReceiveQuota
        IssueWarningQuota=$mb.IssueWarningQuota
        FreeSpace=$freespace
    }
    [pscustomobject]$props
}

$results | Sort-Object TotalItemSize -descending | export-csv c:\script\report.csv -NoTypeInformation -Encoding UTF8

CSV File

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Aviad Ofek
  • 13
  • 4
  • if you are using a serialized data and connecting directly to the exchange server you can use the `TotalItemSize.Value.ToBytes` or other options it has, see this answer: https://stackoverflow.com/a/41716992/4568320 – Avshalom Feb 19 '23 at 10:41

0 Answers0