Use the PowerShell Cmdlet Set-ADUser to change user attributes in Active Directory.
With Set-ADUser you can:
– Change attributes of a single user
– Change attributes of several prefiltered users
– Bulk Change attributes with a CSV file
AD PowerShell Basics
The AD PowerShell Basics series is to show you how to use basic PowerShell commands. Furthermore you learn how to get a lot of information from or to Active Directory with only a little effort and a small script. So the following Cmdlets will be introduced:
New ADUser
Get ADUser
Set ADUser
New ADGroup, Get ADGroup, Set ADGroup
Index
Cmdlet- Set ADUser
The third part of the series AD PowerShell Basics deals with the cmdlet Set-ADUser.
Set-ADUser helps you editing or changing attributes of user accounts in Active Directory.
The cmdlet probably works best in combination with Get-ADUser.
In contrast to the cdmlets from earlier in that series Set ADUser requires some more information. One is the sAMAccountname of the user. The other one is the parameter you want to change.
If you want to change the phone number it might look like this:
Set-ADUser Thomas.Mueller –OfficePhone 01774589252
You could change more than one attribute at once. So you just insert the attributes one after another:
Set-ADUser Thomas.Mueller –OfficePhone 01774589252 –city Frankfurt –streetAddress „Miquelallee 100“
Combining Get-ADUser and Set-ADUser
Now Set-ADUser really gets really useful when you combine it with Get-ADUser. Get-ADUser lets you read all the users you want to change (more info on that on at Get ADUser).
When linked to a pipe (symbol: |) you pass the output to Set-ADUser. Due to this it changes all the relevant attributes of all users. It just requires one line of code.
Situation 1:
Maybe you want to assign the address „Miquelallee 100, Frankfurt“to all AD users named „Miller“.
Get-ADUser –Filter {Surname –eq „Miller“} | Set-ADUser -city Frankfurt –streetAddress „Miquelallee 100“
Situation one was just to show you how it functions. The next situation that relates far more to everyday practice.
Situation 2:
All AD users of the location-OU will get values for the attributes CITY and StreetAddress:
Get-ADUser –Filter * -Searchbase „OU=Testuser,DC=Company,DC=Com“ | Set-ADUser -city Frankfurt –streetAddress „Miquelallee 100“
Changing User Attributes via CSV-Import
You could also change the attributes by importing them from a .CSV file. So this bulk import has the huge advantage that you can mass edit the same attribute for many users with different values. Like different phone numbers that is i.e.
First you import the .csv file with the user data.
(further information: New ADUser – Mass import with CSV )
Next you change the value of the phone number for every user:
$Import =Import-CSV “c:\Test\import.csv”
Foreach ($user in $Import)
{Set-ADUser $user.sAMAccountname –OfficePhone $user.phonenumber}
Finally this will change the phone number one by one for every user.
You could also create a csv. file with Get-ADUser and import it afterwards.
(More Information: Get ADUser – Mass export with CSV).
This really helps you saving time eventually when you have to change the data of many users of your Active directory rather efficiently.
1 Comment
Leave your reply.