Freigeben über


Active Directory Bulk User Modification

An old favourite! This is what automation is all about: making the tedious and the long-winded incredibly easy, and, dare I say, joyous!

Your HR department gives you a dump of user names with email addresses and phone numbers to be updated in Active Directory.

The dump is in the form of a CSV file and looks a something like this:

 

Currently, the users from the dump don't have those values set in Active DIrectory:

 

Now, let's use PowerShell to read in the CSV file and make some bulk updates...

Import-CSV .\HR_Update.csv |

ForEach-Object {Get-ADUser -Filter "Name -eq `"$($_.Name)`"" | Set-ADUser -EmailAddress $_.Email -OfficePhone $_.Phone}

 

First, use the Import-CSV cmdlet to read in the contents of the HR dump file.

Next, the ForEach-Object cmdlet executes the code between the curly braces for each line from the CSV file sent down the pipeline.

The code uses Get-ADUser with a filter that matches the 'Name' from the CSV file column to the name of a user in Active Directory.

If a match is found, the resultant object is piped into the Set-ADUser cmdlet. The Set-ADUser cmdlet updated the user's email address and office phone number based on the current entries from the 'Email' and 'Phone' columns of the CSV file.

 

Let's use PowerShell to check the updates out:

Get-ADUser -Filter * -SearchBase "OU=Word Smiths,OU=User Accounts,DC=fabrikam,DC=com" -Properties EmailAddress,OfficePhone |

Select-Object Name,EmailAddress,OfficePhone

 

 

Well, that's it for now. I hope you enjoyed my take on this stalwart of elementary automation!

Comments

  • Anonymous
    May 14, 2014
    Thanks
  • Anonymous
    July 01, 2015
    Thks! and when the field is empty? o ",,"
  • Anonymous
    September 07, 2015
    The comment has been removed
  • Anonymous
    October 02, 2015
    I tried this but it kicks out question for identity every time i run it

    my code
    import-module ActiveDirectory
    import-CSV C:UsersAdministratorDesktopDC01.csv
    $OU = "OU=Vworkspace,DC=remote,DC=sojab0on,DC=nl"
    $Users = Get-ADUser -Filter * -SearchBase $OU

    ForEach-Object {Set-ADUser -Company $.Bedrijf -OfficePhone $.telephonenumber -MobilePhone $.Mobiel -Department $.Afdeling}

    And whrn i fill in an user identity it looks normal but it does noet change the fields
  • Anonymous
    January 20, 2017
    {Get-ADUser -Filter "Name -eq "$($_.Name)"" | This part doesn't work. PowerShell ISE detect that as an error.
    • Anonymous
      January 14, 2019
      Try this:{Get-ADUser -Filter {Name -eq "$($_.Name)"}
      • Anonymous
        January 14, 2019
        Actually, this:{Get-ADUser -Filter "Name -eq '$($_.Name)'"}
  • Anonymous
    February 12, 2017
    it is very helpful for me.Thank you very much