About Us

Update Active Directory Accounts from a CSV File using PowerShell

by Jess Collicott 6. March 2012 00:27

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:

2012-03-05_1713

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:

1 Import-module ActiveDirectory 2 3 $userList = Import-Csv '.\List of Users.csv' 4 5 foreach ($user in $userList){ 6 Get-ADUser -Filter "SamAccountName -eq '$($user.sAMAccountName)'" -SearchBase "DC=subdomain,DC=company,DC=com" -Properties Company | % { Set-ADUser $_ -Replace @{Company = 'Deliveron'} } 7 }

 

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:

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