I have in my terminate user script, some code that checks the size of the user's 365 mailbox before converting to a shared mailbox. This is the procedure for some clients. If the mailbox is greater than 50GB, the script should notify the admin and not proceed to convert, or else it'll eventually be deleted without a 365 license assigned.
$TotalItemSize = Get-MailboxStatistics $termUserPrincipalName | Format-Table TotalItemSize -hidetableheaders | Out-String
$Value = $TotalItemSize.Split("(")[1].Split(" ")[0].Replace(",","")
If ($TotalItemSize -match "\((?<Size>.*) ")
{
$Value = $Matches.Size.Replace(",","")
}
If ($Value -gt 50gb)
{
write-host "Mailbox is greater than 50GB for $termUserPrincipalName"
}
else
{
write-host "Proceeding to convert mailbox ..."
Set-Mailbox $termUserPrincipalName -Type shared
}
The code works really well most of the time, but with some mailboxes it falsely reads them as greater than 50GB when they are not. I cannot work out why.
Here is my data in excel which for some reason pasted as an image.
Just an update to this one. I've been using this and it works great.
$TotalItemSize = Get-MailboxStatistics $UPN | Format-Table TotalItemSize -hidetableheaders | Out-String
$Value = $TotalItemSize.Split("(")[1].Split(" ")[0].Replace(",","")
if ($TotalItemSize -match "\((?<Size>.*) "){
$Value = $Matches.Size.Replace(",","")
$TotalItemSize = [int64]::Parse($Value)
}
if ($TotalItemSize -gt 50gb){
write-host "Mailbox is greater than 50GB for $UPN"
}
else{
write-host "Proceeding to convert to Shared Mailbox"
}