HOW TO: Enable Mailboxes in different Storage Groups based on first character of First Name using Powershell script

In case you have already created the AD accounts and wants to enable mailbox and have a requirement that the mailboxes should be created in different Storage Groups based on the First character of the First Name, you can use the method shown below:

In this case the Storage Groups are named as follows:
A-B Users
C-D Users
E-F Users
G-H Users
....
The list of the users are available in a text file. Format of the text file is as below:

The First row denotes the Field Names.The file is named UserAccounts.txt
Account,First,Last
rjohn,Robin,John

Create a .PS1 file as below:

 #Gets the Server Name
$server = [System.Environment]::MachineName 

#Domain Name
$domain = "MyCompany" 

import-csv UserAccounts.txt |
    foreach{ 
        #Create the ID
        $id = $("$domain"+"\"+$_.Account);
        $alias = $_.Account 

        #Get the first character of the first name
        $str =$_.First.substring(0,1)
        $Database="" 

        #Set the Database based on the first character of the first name
        #You might need to change the name of the Storage Group and Database based on
        #your environment. 

        switch -regex ($str.ToLower()) 
         { 
                "[a-b]" {$Database = $server+"\A-B Users\"+"Mailbox Database"} 
                "[c-d]" {$Database = $server+"\C-D Users\"+"Mailbox Database"} 
                "[e-f]" {$Database = $server+"\E-F Users\"+"Mailbox Database"} 
                "[g-h]" {$Database = $server+"\G-H Users\"+"Mailbox Database"} 
                "[i-j]" {$Database = $server+"\I-J Users\"+"Mailbox Database"} 
                "[k-l]" {$Database = $server+"\K-L Users\"+"Mailbox Database"} 
                "[m-n]" {$Database = $server+"\M-N Users\"+"Mailbox Database"} 
                "[o-p]" {$Database = $server+"\O-P Users\"+"Mailbox Database"} 
                "[q-r]" {$Database = $server+"\Q-R Users\"+"Mailbox Database"} 
                "[s-t]" {$Database = $server+"\S-T Users\"+"Mailbox Database"} 
                "[u-v]" {$Database = $server+"\U-V Users\"+"Mailbox Database"} 
                "[w-x]" {$Database = $server+"\W-X Users\"+"Mailbox Database"} 
                "[y-z]" {$Database = $server+"\Y-Z Users\"+"Mailbox Database"}
                default {$Database = $server+"\First Storage Group\"+"Mailbox Database"}
         }
        # Run the actual command to enable the mailbox.
        enable-Mailbox -Identity $id -Database $Database -Alias $alias
        -DisplayName $($_.First+ " " + $_.Last)

  }

To Run the script:

1)Open the Exchange Management Shell

2)Navigate to the location where you have stored the Data and the script file.

3)Type in .\ScriptName.PS1 and hit enter to execute the script.