Changing UserName and SMTP addresses in bulk

Recently I had to perform a task in which there was a need to change UserName, Alias and SMTP addresses associated with the user for approx 150 users. These were recently created users.

If I start doing it using GUI, am sure my whole day will pass in find, click.. click.. click..

I had to do little bit of manual work like generating a csv output for the users in the department OU and add new username and new email address column to it.

The final CSV looked like this.
image

Once ready with the csv file, lets import it in PowerShell and let it do its magic.

$userCol = Import-Csv -Path C:\Update-users.csv
Foreach($user in $userCol){
"Processing : "+$user.UserName
    Set-Mailbox -Identity $user.UserName -EmailAddressPolicyEnabled $false
    Set-Mailbox -Identity $user.UserName -Alias $user.NewUserName
    Set-Mailbox -Identity $user.UserName -EmailAddresses $user.NewEmail
    Set-Mailbox -Identity $user.UserName -EmailAddressPolicyEnabled $true
"Changing UserName from: "+$user.UserName+" TO: "+$user.NewUserName
    Set-Mailbox -Identity $user.UserName -SamAccountName $user.newUserName -UserPrincipalName $user.upn -Alias $user.NewUserName
    }

I did use a seperate set-mailbox for each task for easy to understand.

The script is self explanatory.

Hope it helps.

Advertisements