Created
January 5, 2015 19:41
-
-
Save TScalzott/4964af719da46f75cc0e to your computer and use it in GitHub Desktop.
Get-MailboxSizes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get-MailboxSizes.ps1 | |
# | |
# Requires Microsoft Active Directory module | |
If ((Get-Module | Where { $_.Name -eq "ActiveDirectory"}) -eq $null) { | |
Import-Module ActiveDirectory; | |
If ((Get-Module | Where { $_.Name -eq "ActiveDirectory"}) -eq $null) { throw "ActiveDirectory Module is required." } | |
} | |
$colServers = @("Server1", "Server2") # array of Exchange Servers to poll | |
$strGroup = "CN=O365-Migrated,CN=YourOU,DC=domain,DC=com" # dn of security group used to track those already migrated | |
$colDCs = @("domain1-dc") # one more more domain controllers -- in case spanning domains | |
$strCsvFile = "mailbox-sizes.csv" | |
# get all mailboxes, their size, their number of items | |
$colBoxes = @() | |
ForEach ($server in $colServers) { | |
Write-Host "Getting mailboxes from $($server)..." | |
$colBoxes += Get-WMIObject -namespace "root\MicrosoftExchangeV2" -class "Exchange_Mailbox" -Computer $server ` | |
-Filter "NOT MailboxDisplayName like 'System%' and NOT MailboxDisplayName like 'SMTP%'" ` | |
| Select-Object ServerName, MailboxDisplayName, @{N="Size";E={"{0:N0}" -f [int]($_.Size/1024)}}, @{N="Items";E={"{0:N0}" -f $_.TotalItems}} | |
} | |
# turn all of that into custom objects | |
$colDetails = @(); | |
$colBoxes | % { $colDetails += New-Object PSObject -Property @{ Server = $_.ServerName; Mailbox = $_.MailboxDisplayName; Size = $_.Size; ` | |
Items = $_.Items; Department = ""; Office = ""; ForCSV = ""; ActiveMbx = "" } } | |
Write-Host "Getting accounts. This can take a while." | |
$colDetails | ForEach-Object { | |
$mbx = $_ | |
$name = $mbx.Mailbox | |
ForEach ($strDC in $colDCs) { | |
$user = Get-ADUser -Server $strDC -Filter { DisplayName -like $name } -Properties DisplayName, MemberOf, Enabled, Department, Office, mail | |
if ($user) { | |
$mbx.Department = $user.Department | |
$mbx.Office = $user.Office | |
$mbx.ForCSV = "$($user.mail),,FALSE" | |
$mbx.ActiveMbx = ($user.Enabled -eq $true) -and ($user.Memberof -notcontains $strGroup) | |
break | |
} | |
} | |
} | |
$coldetails | Select-Object Mailbox, Server, @{N="Size (MB)";E={$_.Size}}, Items, Office, Department, ActiveMbx, ` | |
@{N="EmailAddress,Password,ForceChangePassword";E={$_.ForCSV}} | Sort-Object Mailbox | Export-Csv $strCsvFile | |
Write-Output Wrote $strCsvFile | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment