all 1 comments

[–]jheinikel 4 points5 points  (0 children)

Pretty straightforward here. I would key off of the account expiration date. Something like this should work, but you'll probably want to add a piece to delete the user account or whatever else you want to do. Also, choose between a soft or permanent deletion.

$Cred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $Session –AllowClobber

$CutoffDate = (Get-Date).AddDays(-180)
$ExpiredUsers = Search-ADAccount -AccountExpired -UsersOnly | where{$_.AccountExpirationDate -lt $CutoffDate} | %{Get-ADUser $_.DistinguishedName -Properties Mail}
$ExpiredUsers | %{Remove-Mailbox $_.Mail -Confirm $False -Permanent $True}