Skip to content

Instantly share code, notes, and snippets.

@warfighter8
Created July 5, 2018 17:33
Show Gist options
  • Save warfighter8/cb000d2529c73ca930a186bf37f5cc97 to your computer and use it in GitHub Desktop.
Save warfighter8/cb000d2529c73ca930a186bf37f5cc97 to your computer and use it in GitHub Desktop.
################################################################################################################################################################
# To run the script
#
# .\Get-MailBoxSizesOU.ps1
#
# NOTE: If you do not pass an input file to the script, it will return the sizes of ALL mailboxes in the tenant. Not advisable for tenants with large
# user count (< 3,000)
#
#Supports Modern Authentication and Multi Factor Authenication
#Dependenices: Exchange Online Module
#
# Author: Terry Wensel
# Version: 1.0
# Last Modified Date: 7/5/2018
# Last Modified By: Terry Wensel
################################################################################################################################################################
#Constant Variables
$OutputFile = "C:\temp\MailboxSizes.csv" #The CSV Output file that is created, change for your purposes
$SearchBase = 'OU=Users,OU=United Kingdom,OU=Locations,DC=eriezusa,DC=net'
#Main
Function Main {
#Remove all existing Powershell sessions
Get-PSSession | Remove-PSSession
#Call ConnectTo-ExchangeOnline function with correct credentials
ConnectTo-ExchangeOnline
#Prepare Output file with headers
Out-File -FilePath $OutputFile -InputObject "UserPrincipalName,NumberOfItems,MailboxSize" -Encoding UTF8
#No input file found, gather all mailboxes from Office 365 where the accounts are in a specific OU
$objUsers = Get-ADUser -Filter * -SearchBase $SearchBase | select-object UserPrincipalName
#Iterate through all users
Foreach ($objUser in $objUsers)
{
#Connect to the users mailbox
$objUserMailbox = get-mailboxstatistics -Identity $($objUser.UserPrincipalName) | Select ItemCount,TotalItemSize
#Prepare UserPrincipalName variable
$strUserPrincipalName = $objUser.UserPrincipalName
#Get the size and item count
$ItemSizeString = $objUserMailbox.TotalItemSize.ToString()
$strMailboxSize = "{0:N2}" -f ($ItemSizeString.SubString(($ItemSizeString.IndexOf("(") + 1),($itemSizeString.IndexOf(" bytes") - ($ItemSizeString.IndexOf("(") + 1))).Replace(",","")/1024/1024)
$strItemCount = $objUserMailbox.ItemCount
#Output result to screen for debuging (Uncomment to use)
#write-host "$strUserPrincipalName : $strLastLogonTime"
#Prepare the user details in CSV format for writing to file
$strUserDetails = "$strUserPrincipalName,$strItemCount,$strMailboxSize"
#Append the data to file
Out-File -FilePath $OutputFile -InputObject $strUserDetails -Encoding UTF8 -append
}
#Clean up session
Get-PSSession | Remove-PSSession
}
###############################################################################
#
# Function ConnectTo-ExchangeOnline
#
# PURPOSE
# Connects to Exchange Online Remote PowerShell using the tenant credentials
#
# RETURN
# None.
#
###############################################################################
function ConnectTo-ExchangeOnline
{
#Create remote Powershell session
$modules = @(Get-ChildItem -Path "$($env:LOCALAPPDATA)\Apps\2.0" -Filter "Microsoft.Exchange.Management.ExoPowershellModule.manifest" -Recurse )
$moduleName = Join-Path $modules[0].Directory.FullName "Microsoft.Exchange.Management.ExoPowershellModule.dll"
Import-Module -FullyQualifiedName $moduleName -Force
$scriptName = Join-Path $modules[0].Directory.FullName "CreateExoPSSession.ps1"
. $scriptName
$null = Connect-EXOPSSession
$session = (Get-PSSession | Where-Object { ($_.ConfigurationName -eq 'Microsoft.Exchange') -and ($_.State -eq 'Opened') })[0]
#Import the session
Import-PSSession $Session -AllowClobber | Out-Null
}
# Start script
. Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment