jheinrichs79

Get-MailboxSize

Nov 21st, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Connect-EXOnline {
  2.     param (
  3.         [string]$Credential
  4.     )
  5.  
  6.     $credentials = Get-Credential -Credential $Credential
  7.     Write-Output "Getting the Exchange Online cmdlets"
  8.  
  9.     $Session = New-PSSession -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
  10.         -ConfigurationName Microsoft.Exchange -Credential $credentials `
  11.         -Authentication Basic -AllowRedirection
  12.     Import-PSSession $Session
  13.  
  14. }
  15.  
  16. Function disconnect-EXOnline {
  17.     $session = Get-PSSession
  18.     foreach ($id in $session) {
  19.         if ($id.ConfigurationName -eq 'Microsoft.Exchange') {
  20.             Remove-PSSession $session      
  21.         }
  22.     }
  23. }
  24.  
  25. function get-MailboxSize {
  26.     param (
  27.         [string]$UserMailbox
  28.     )
  29.     $CheckConnection = Get-PSSession
  30.     cls
  31.     if($null -eq $CheckConnection){
  32.         Connect-EXOnline
  33.         cls
  34.     } else {
  35.         if ($CheckConnection.ConfigurationName -ne "Microsoft.Exchange"){
  36.         Connect-EXOnline
  37.         cls
  38.         }
  39.     }
  40.  
  41.     $totalItemSize = Get-MailboxStatistics $UserMailbox | Select-Object TotalItemSize
  42.     $totalItemSize = ($totalItemSize.TotalItemSize.value).tostring()
  43.     $totalItemSize = $totalItemSize.trimend(" bytes)")
  44.     $totalItemSize = $totalItemSize.Split("(")
  45.     [string]$totalItemSize = $totalItemSize[1].Trim()
  46.     $totalItemSize = $totalItemSize.Replace(',', '')
  47.     [uint64]$MailboxSize = $totalItemSize / 1MB
  48.  
  49.     Write-Host $UserMailbox
  50.     Write-host "Mailbox Size (MB):"
  51.  
  52.     $TotalItemSize = $null
  53.     return $MailboxSize
  54. }
  55.  
  56.  
  57. #UserMailbox is the Mailbox you want to find the size of.
  58. get-MailboxSize -UserMailbox '<Enter Email Address>'
Add Comment
Please, Sign In to add comment