Update Active Directory Accounts from a CSV File using PowerShell

Share on facebook
Share on twitter
Share on linkedin

For instances where there is need to update a specific list of user accounts in Active Directory, it may be ideal to use PowerShell to loop through the list and make the changes for each of the users using the Active Directory Administration Module for PowerShell. For the sake of this example, assume there is a CSV file with a column titled sAMAccountName:


If we wanted to update the Company attribute in AD for each of these users, we could use the following PowerShell script to read in the list of users, and use Get-ADUser to get the specific AD account object, and then pass to Set-ADUser to make the actual change:

Import-module ActiveDirectory

$userList= Import-Csv '.\List of Users.csv'

foreach ($userin$userList){
Get-ADUser -Filter"SamAccountName -eq '$($user.sAMAccountName)'"-SearchBase "DC=subdomain,DC=company,DC=com"-Properties Company |% { Set-ADUser $_-Replace@{Company = 'Deliveron'} }

If you then wanted to query AD for those users to make sure they updated correctly, you could use the following query using Get-ADUser:

foreach ($userin$userList){
Get-ADUser -Filter"SamAccountName -eq '$($user.sAMAccountName)'"-SearchBase "DC=subdomain,DC=company,DC=com"-Properties Company | Select SamAccountName, Name, Company

More to explore