Export and Disable Exchange Mailboxes With Specific Attributes Using PowerShell

I have been working on archiving and removing a a little over 3000 Exchange Mailboxes that had been sitting since my client's move to Google Apps. Obviously, I didn't want to manually go through and export and disable all of the mailboxes, so I made a quick and dirty script which I thought I would share here. Emphasis on quick and dirty here. :)

Import-Module activedirectory

#get objects to work with. Use the Database parameter to point to a specific DB, and the ResultSize parameter to specify how many to pull at a time.
$userstobackup = Get-Mailbox -Database DAG01-DB01 -ResultSize 100 | where {$_.CustomAttribute1 -like "google" -and $_.ServerName -like "*exmb*" -and $_.Name -notlike "DiscoverySearchMailbox*"}
$userstobackup.length
 
#Enable MAPI (Required to do the export)
ForEach ($user in $userstobackup) {
 $username = $user.SamAccountName
 Set-CASMailbox -verbose -Identity $username -MAPIEnabled $true
}
#Wait 60 seconds (to allow MAPI settings to propagate)
Start-Sleep -s 60
 
#start export for each user
ForEach ($user in $userstobackup) {
 $username = $user.SamAccountName
 New-MailboxExportRequest -verbose -Mailbox $username -FilePath \\archiver01\2010_export\GoogleUsers\$username.pst
}

###########################################################################################################################
##Wait until Mailbox Exports are all complete
###########################################################################################################################
 
#backup the user attributes to CSV, then disable mailbox
ForEach ($user in $userstobackup) {
 $username = $user.SamAccountName
 $proxyAddresses = $user.EmailAddresses
 $email = $user.PrimarySmtpAddress.ToString()
 #export user object properties
 $userobject = $user.samaccountname.ToString() + "," + $user.PrimarySmtpAddress.ToString() + "," + $user.CustomAttribute1.ToString()
 ForEach ($address in $ProxyAddresses) {
  if ($address -like "*SMTP*") {
   $userobject = $userobject + "," + $address.ToString()
  }
 }
 $userobject >> \\archiver01\2010_export\GoogleUsers\exportedusers.csv
 #disable mailbox
 Disable-Mailbox -Identity $username -Confirm:$False
}
#wait for attribute changes to sync
Start-Sleep -s 60
#re-add removed attributes
ForEach ($user in $userstobackup) {
 $username = $user.SamAccountName
 $proxyAddresses = $user.EmailAddresses
 $email = $user.PrimarySmtpAddress.ToString()
 Set-ADUser -Identity $username -Replace @{mail="$email";extensionAttribute1="google"}
 ForEach ($address in $proxyAddresses) {
  if ($address.ToString() -like "*SMTP*") {
   Set-ADUser -Identity $username -Add @{proxyAddresses=$address.ToString()}
  }
 }
}


#you can run this to verify the fields are present
ForEach ($user in $userstobackup) {
 get-aduser $user.SamAccountName -Properties mail,extensionattribute1,proxyaddresses
}

#you can run this to verify the fields are present - improved to make it slightly more helpful and entirely more sloppy
ForEach ($user in $userstobackup) {
 get-aduser $user.SamAccountName -Properties mail,extensionattribute1,proxyaddresses | ft SamAccountName,mail,extensionAttribute1,proxyAddresses
}

 

Tutorial Tags: