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

6 thoughts on “Changing UserName and SMTP addresses in bulk

  1. How to update the country for the user profile in AD. If you have a list of users who are from different country and country need to update in AD. this scenario happens when creating new joiner accounts of different region.
    So do know how to use powershell to update country, when creating new joiner accounts.

      • All other properties like Street, address, zipcode, updates but does not update country. if you have test environment, please do test.

      • Did you use Country abbreviation or Country name as value? you must use abbreviation.
        e.g., Set-ADUser -Identity TestUser01 -Country “BH”

      • Here is my complete powershell code, it works for all attributes, but not for country.
        I could not add the excel sheet, Hope you will take dig into this code.

        # Import AD Module
        Import-Module ActiveDirectory

        write-Host ‘Starting to update AD Attributes…….’ -NoNewline -ForegroundColor Yellow
        # Import CSV into variable $users

        $users = Import-Csv -Path C:\temp\users.csv
        # Loop through CSV and update users if the exist in CVS file

        foreach ($user in $users) {
        #Search in specified OU and Update existing attributes
        Get-ADUser -Filter “SamAccountName -eq ‘$($user.samaccountname)'” -Properties * -SearchBase “DC=ittechsolution,DC=com” |Set-ADUser -Country $Country }
        #Set-ADUser -Office $($user.Office) -OfficePhone $($user.officePhone) -MobilePhone $($user.MobilePhone) -fax $($user.fax) -Title $($user.Title) -Department $($user.Department) -Company $($user.Company) -Manager $($user.Manager) -StreetAddress($user.StreetAddress) -City $($user.City) -State $($user.State) -PostalCode $($user.PostalCode) -Replace @{IPPhone=$User.IPPhone} -add @{OtherTelephone=$user.OtherTelephone},-Country $($user.Country)
        }
        Write-Host ‘done!’ -ForegroundColor Green

      • The problem that I see is, you are importing user accounts to be processed from CSV as well as retrieving users using “Get-ADUser” within Foreach loop.

        This is how my CSV looks.
        “Name”,”samaccountname”,”Country”
        “Lab User A”,”labusera”,”BH”
        “Lab User B”,”labuserb”,”IN”
        “Lab User C”,”labuserc”,”US”
        “Lab User D”,”labuserd”,”UK”

        #—Script—
        $users = Import-Csv -Path C:\reports\userlist.csv

        foreach($user in $users){
        #SAMAccountName should be in CSV
        Set-ADUser $user.samaccountname -Country $User.country
        }

        #check Output
        Get-ADUser -Filter * -Properties * | where{$_.Name -like “Lab*”} | select Name,SamAccountname,Country

        #—Output—
        Name SamAccountname Country
        —- ————– ——-
        Lab User A labusera BH
        Lab User B labuserb IN
        Lab User C labuserc US
        Lab User D labuserd UK

        Hope this helps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s