PowerShell V2 script to update Active Directory users from a CSV file. Only specified fields in the CSV that are not missing update the users. The value "<delete>" flags to clear the attribute. Attributes are not updated if the value in the CSV matches the existing value in AD.
Just about any database of users can be exported to a comma delimited (CSV) file. This script ignores missing values in the CSV file. It only considers fields in the CSV file that you specify. And it only updates a user if the new value specified in the CSV file does not match the existing value in Active Directory. This means a comma delimited file can be used to repeatedly update users in bulk. There is no need to ensure that the file only includes new updates. In fact, there is not even a requirement that each user have only one row in the CSV file.
Features of the script:
The CSV must have a header line. The field labeled UserID identifies the users in Active Directory. The value in the UserID field can be any of the following: sAMAccountName (pre-Windows 2000 logon name), distinguishedName, SID, or GUID. The remaining fields of the CSV that will be used to update users are specified in the script by the array $Fields. These fields must be identified by either the LDAPDisplayNames of the Active Directory attributes or the names of the supported Set-ADUser parameters. The $Fields array should not include the field named UserID. Any fields of the CSV not specified in the array are ignored (except UserID).
All of the updates for each user (one row of the CSV file) are done in one or two PowerShell statements, using the Set-ADUser cmdlet. For each row the script populates the hash table $AttrReplace with the field names and the new values to be passed to the -Replace parameter of the Set-ADUser cmdlet. Similarly, the array $AttrClear is populated with the field names to be cleared for the user using the -Clear parameter. If any supported parameters are specified they are added to the cmdlet.
The script also demonstrates how to construct a PowerShell command as a string. The script appends various parameters to the string as required. A series of conditional statements determine which parameters are needed for every line in the CSV file. The resulting string is then converted into a script block using the Create method of the .NET scriptblock class. The script block is passed to the Invoke-Command cmdlet, which runs the PowerShell command on the local client. However, this technique cannot be used to add the -Replace parameter to the command. The required hash table cannot be converted from a string into a hash table when the string is converted into a script block. A separate Set-ADUser command is required to employ the -Replace parameter.
Each time the Set-ADUser cmdlet is run by the script, there are 7 possible parameters: -Replace, -Clear, -AccountExpirationDate, -Enabled, -Manager, -PasswordNeverExpires, and -SmartcardLogonRequired. We can add all but the first to a string representation of a Set-ADUser command. If the script simply checked which of the 6 remaining parameters were needed and ran separate Set-ADUser commands for each possibility, there would be 32 possible combinations. One combination results in no update for the user. The script would require 31 separate Set-ADUser commands, plus the code to determine which to use for each line of the CSV file. Instead, the script uses a few conditional statements to build the PowerShell command as a string. The string must be converted into a script block before it can be executed by the Invoke-Command cmdlet.
The script writes a detailed log documenting the result of each line in the CSV. The script also logs the number of CSV lines processed, the number of users updated, the number not updated, the number of users not found, and the number of errors raised attempting to update users.
Suggested steps to use the script:
Cautions:
An example CSV file follows:
UserID,title,Enabled,manager,sAMAccountName,otherTelephone
srbecker,Temp Contractor,,"cn=Jim Smith,ou=Staff,dc=MyDomain,dc=com",,222-123-4598
jmsmith,"<delete>",False,,,222-123-4567
rmbecker,,,,,"<delete>"
fjohnson,IT Coordinator,True,,NewNTName,
swilliams,Help Desk,,jsmith,,222-456-8765