Replace attributes for multiple users in Active Directory

In this example, we will bulk change some of the fields for multiple users in Active Directory.

In this case we want to change the city, state, office, street address, postal box and postal code for all users matching a certain criteria.

🛑 Warning!
Be careful when using this script, as it will apply changes to your users. The -WhatIf option is added to avoid disasters. Remove -WhatIf only when you feel ready to actually apply the script!

So this is what the script looks like. Let me explain what it does.

But first of all replace -Credential abc.local\Administrator with the domain and user by which you want to execute the script. The -server servername.abc.local should match your server that is running Active Directory services.

$users = Get-ADUser -Credential abc.local\Administrator -server servername.abc.local -SearchBase "OU=Users,OU=ABCompany,DC=abc,DC=local" -Filter {(city -like "Shenzhen") -And (Enabled -eq $true)}

foreach ($user in $users) {
    Set-ADUser $user.samaccountname -city "......" -State "......" -Office "......" -streetaddress "......" -POBox "......" -Postalcode "......" -WhatIf
}

First, it finds all the users for which we want to make the changes and stores them as a variable called $users.

The -SearchBase specifies in what OU/folder in Active Directory we are looking in. Make sure it matches your structure.

It also narrows down the search by using a filter, matching only users that are enabled and have Shenzhen in the city field.

Then, for each user, it has found, it changes the city, state, office, streetaddress, POBox and Postalcode fields to something of your choice. In this case just some dots for example purposes.

Of course you will have to replace ...... with the actual value you want to set to the users.

When you get the grasp of it and feel ready to execute the script you can go ahead and remove -WhatIf to apply the script.

Leave a Reply

Your email address will not be published. Required fields are marked *