Sunday , December 17 2017
Home - Development - Updating Active Directory User Objects from CSV files

Updating Active Directory User Objects from CSV files

Sometimes it’s better to get updates for Active Directory objects supplied in the form of a Excel spreadsheet.  Using the information you can then run the following script to update objects with information contain the rows/columns of a spreadsheet (CSV file).  What makes this a little simplier the column headers are the AD properties to be udpated.

Another issue covered in the following script is the lack of cell data in a row.  The commnd to update the AD object is built for each row of the CSV file and invoked with the updated information only.  i.e. Cell B4 is empty

The following powershell script can be adapted for any AD Property or AD Object type updatable from powershell:

foreach ($record in (Import-Csv namestest.csv)) {
  $user = $record.samAccountName
  $command = “Set-ADUser -identity $user”

  foreach ( $attr in (Get-Member -InputObject $record -MemberType NoteProperty) ) {
     $value = $record.($attr.Name)
     if ( $value -and ( $attr.Name -ne ‘samAccountName’ ) ) {
          $command += ” -” + $attr.Name + ” ‘” + $value + ”’”
     }
   }
  write-host $command
  Invoke-Expression $command
}

This requires powershell V2 with the Active Directory Management module imported to use the Set-AdUser command.  This can also be used with Exchange 2010 PS, for example the set-mailbox or set-mailcontact commands.

 

Check Also

Creating Dynamic Distribution Groups

Ive been asked to create a Distribution Group based on Users City Value.  The following …

Leave a Reply

Your email address will not be published. Required fields are marked *