Tech Toolbox #1: AD Update
Introduction
Between the two technical support jobs I’ve had so far, the one thing I’ve felt necessary to take from job to job is a good set of scripts. The reason is that we are often required in our active daily duty to perform repetitive tasks that can more often than not be automated.
The script that I most recently created is on that is essential for me to use, an Active Directory Update script. The whole purpose of this is to provide an easy and visual way of updating user records in a windows based domain.
For me the obvious method and easy method of entering data would be to use a CSV file. Powershell already has functions for parsing the data into an array so that saves on us having to program that function. It then matches the users up via the SAMAccount name and then only updates the values that have data in them. You can even easily modify the script to work with custom attributes!
The Script
The script has a few variables that need entering in before you can run it to any effect:
# User Defined Variables
$csvlocation = "c:\ADUPDATE_TEST.csv"
$domain = "dc=domain,dc=com"
Next the script imports the PowerShell 2.0 module ActiveDirectory, this allows us to interface with the Domain Controller. After this, the next thing to do is import the CSV into an array we do this via the following line:
$users = Import-Csv -Path $csvlocation
The above line is the reason we’re using PowerShell - in one line we can import and assign an array values from an external file. Using this we run through the array one user at a time:
foreach ($user in $users) {
# Code Goes Here
}
Inside the for loop is where we update the user from the CSV file. The first thing we need to do is, load in the account name. We do this by using the userPrincipalName field in the CSV file and importing this into a variable called $SamAc. From this we find the user to update by matching up the sAMAccountName with the variable we created above. The line below uses the Get-ADUser function and passes in the filter “sAMAccountName -eq ’$SamAc’”, this is then searched against the domain specified in the variables above.
$usertoupdate = Get-ADUser -Filter "sAMAccountName -eq '$SamAc'" -SearchBase $domain
We now have a user file that we want to update stored in $usertoupdate, this means we can now start applying values to the user class. Below is an example and can be customised to suit various values that you may need to update. It checks to see if there is a value in the $user object pulled in from the CSV. If there is a value then the corresponding value in $usertoupdate is set equal to the value in the $user object. The example below shows the setting of the department variable:
if ($user.department -ne "") {
$usertoupdate.department = $user.department
}
The final step is to set the values we've assigned to the user $usertoupdate. We do this through one simple line - it sets the ADUser equal to an instance specified:
Set-ADUser -Instance $usertoupdate
The only thing left to do is repeat that process for each of the users in the CSV file.
This is the first one of these that I have done, hence the #1 in the title, however there is a metric tonne of Scripts and code snippets that I have created to make my life as a technician easier so I'll be looking to share these snippets in the future.
If you want to download the script then follow this link to my GitHub page.
GitHub Link











