PoshCode Logo PowerShell Code Repository

Spread-Mailboxes.ps1 by Jan Egil Ring 7 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1839"></script>download | new post

Script to spread mailboxes alphabetically across mailboxdatabases based on the first character in the user`s displayname.
For more information, see the following blog-post: http://blog.powershell.no/2010/05/14/script-to-spread-exchange-mailboxes-alphabetically-across-databases

  1. ###########################################################################
  2. #
  3. # NAME: Spread-Mailboxes.ps1
  4. #
  5. # AUTHOR: Jan Egil Ring
  6. # EMAIL: jer@powershell.no
  7. #
  8. # COMMENT: Script to spread mailboxes alphabetically across mailboxdatabases based on the first character in the user`s displayname.
  9. #          For more information, see the following blog-post: http://blog.powershell.no/2010/05/14/script-to-spread-exchange-mailboxes-alphabetically-across-databases
  10. #
  11. # You have a royalty-free right to use, modify, reproduce, and
  12. # distribute this script file in any way you find useful, provided that
  13. # you agree that the creator, owner above has no warranty, obligations,
  14. # or liability for such use.
  15. #
  16. # VERSION HISTORY:
  17. # 1.0 14.05.2010 - Initial release
  18. #
  19. ###########################################################################
  20.  
  21. #Add the Exchange Server 2010 Management Shell snapin
  22. Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue
  23.  
  24.  
  25. #Loop through each mailbox
  26. foreach ($mailbox in (Get-Mailbox -ResultSize unlimited)) {
  27.  
  28. $displayname = $mailbox.Displayname
  29.  
  30. #Determine which database the mailbox should reside on. Displaynames not matching [A-Z] on first character goes to the last database.
  31. switch -regex ($displayname.substring(0,1))
  32.     {
  33.        "[A-F]" {$mailboxdatabase = "MDB A-F"}
  34.        "[G-L]" {$mailboxdatabase = "MDB G-L"}
  35.        "[M-R]" {$mailboxdatabase = "MDB M-R"}
  36.        "[S-X]" {$mailboxdatabase = "MDB S-X" }
  37.        "[Y-Z]" {$mailboxdatabase = "MDB Y-Z" }
  38.         default {$mailboxdatabase = "MDB Y-Z" }
  39.     }
  40.  
  41. #Move mailbox to the correct database if not already correct.
  42. if (($mailbox.database.name) -ne $mailboxdatabase) {New-MoveRequest -Identity $mailbox -TargetDatabase $mailboxdatabase;Write-Host "Moving $displayname to $mailboxdatabase" -ForegroundColor Green}
  43.  
  44. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me