Update Active Directory Accounts from a CSV File using PowerShell

Mar 06, 2012

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:

1 Import-module ActiveDirectory 2 3 $userList= Import-Csv '.\List of Users.csv' 4 5 foreach ($userin$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 ($userin$userList){ 2 Get-ADUser -Filter"SamAccountName -eq '$($user.sAMAccountName)'"-SearchBase "DC=subdomain,DC=company,DC=com"-Properties Company | Select SamAccountName, Name, Company 3 }

Tags: powershell 2.0
Category: PowerShell
Fatal error: Call to a member function getUserUserBio() on a non-object in /home/deliveron/wwwroot/packages/problog/helpers/blogify.php on line 362