Friday, May 24, 2013

Powershell script to Delete/Remove AD user account & Exchange mailbox

1. Create the file ADUserDel.ps1 .  Will dump the mailbox to the location, \\SERVERNAME\SHARENAME\mailbox.pst

$Name = Read-Host "Please enter a mailbox to archive"
New-MailboxExportRequest -Mailbox $Name -FilePath \\SERVERNAME\SHARENAME\$Name.pst
while ((Get-MailboxExportRequest -Mailbox $Name | Where {$_.Status -eq "Queued" -or $_.Status -eq "InProgress"}))
{
sleep 60
}
Get-MailboxExportRequest -Mailbox $Name | Remove-MailboxExportRequest -Confirm:$false
Remove-Mailbox -Identity $Name -Confirm:$false

2. Execute the file from within the Exchange Shell