Change Multiple Active Directory Users Attributes Using PowerShell

Two day ago I published an article about how to get User Information using the Get-ADUSER cmdlet and today I’ll show how you can modify users Information using the SET-ADUSER.

The SET-ADUSER In another Core cmdlet In the Active Directory PowerShell Module and It’s very powerful when there Is a need to modify multiple users.

In this article I’ll show how I’m changing multiple Active directory Users attributes using PowerShell query.

As you will see below, I’m going to add a code to all my Nano Server admins using a query that will search for all users with the tittle Nano Admins.

If the title Is correct a code40 value will added to the admindescription attribute.

Get-ADUser -filter "title -like 'Nano admins'" | set-aduser -add @{"admindescription"="Code40"}

 Get-ADUser -filter "title -like 'Nano admins'" -properties * | select name, title

This how the user properties looks like after the change.

To reverse the change type

Get-ADUser -filter "title -like 'Nano admins'" | set-aduser -remove @{"admindescription"="Code40"} -verbose

You can also use a .CSV file to make changes to a number of users In the file as seen below:

Import-Csv .\users.csv | foreach{Set-ADUser $_.user -Title "Nano Admin" -PassThru } | ft

This is how the .CSV file looks like

And If I open the properties of the users I can see the new Job Title

And below you will see another flavour of the cmdlet, Where I get all the users with Nano Admin title and set their description name to Administrator

Get-ADUser -Filter 'Title -like "Nano Admin"' | Set-ADUser -description "administrator"

In the example below I’ll show you the core function of the Set-Module which they are:

Add details to users

Remove details

Clear (reset , set to null)

Replace (change existing details)

I can also replace \ change user details

Get-ADUser -Filter 'Title -like "Nano Admin"' | Set-ADUser -Replace @{description="Enginer";department="42"}

Get-ADUser -Filter 'Title -like "Nano Admin"' -Properties * | select name,title,description,department

To remove details from multiple users:

Get-ADUser -Filter 'Title -like "Nano Admin"' | Set-ADUser -remove @{description="Enginer";department="42"}

Get-ADUser -Filter 'Title -like "Nano Admin"' -Properties * | select name,title,description,department

And to add value to multiple users type:

Get-ADUser -Filter 'Title -like "Nano Admin"' | Set-ADUser -add @{description="Enginer";department="42"}

Get-ADUser -Filter 'Title -like "Nano Admin"' -Properties * | select name,title,description,department

To clear Value and set them to Null use the code below:

Get-ADUser -Filter 'Title -like "Nano Admin"' | Set-ADUser -clear description,department

Get-ADUser -Filter 'Title -like "Nano Admin"' -Properties * |select name,description,department

And In the ultimate code below, I’m setting the mail attrivute of all the users In my Dev OU to be Firstname.Lastname@test.local

Get-ADUser -Filter 'Name -like "*"' -SearchBase 'OU=dev,DC=test,DC=local' -Properties * | % {Set-ADUser $_ -email ($_.GivenName + '.' + $_.Surname +"@test.local")}

Get-ADUser -Filter 'Name -like "*"' -SearchBase 'OU=dev,DC=test,DC=local' -Properties * | select name,mail

And In my final and example I’m setting \ adding a primary SMTP address based on Firstname.Lastname@test.local

Get-ADUser -Filter 'Name -like "*"' -SearchBase 'OU=dev,DC=test,DC=local' -Properties * | % {Set-ADUser $_ -add @{proxyAddresses="SMTP:"+ $_.GivenName + '.' + $_.Surname +"@test.local"}}

Get-ADUser -Filter 'Title -like "Nano Admin"' -Properties * | select name,proxyAddresses

Leave a Reply