Mass Power Shell AD update from CSV file

By SuperG - Last updated: Tuesday, April 3, 2012 - Save & Share - Leave a Comment

Make mass changes to AD using Power Shell AD and CSV file

You can update almost anything in AD using power shell. Here I will show how to update the company field of a user object.

First look at this page for information if your power shell script does not work with the following error:

“.ps1 cannot be loaded because the execution of scripts is disabled on this system.”

http://technet.microsoft.com/en-us/library/ee176949.aspx

This error has to do with the ExecutionPolicy, I will not tell you what to set this to as each company has its own policies on running scripts. So make sure you follow your companies policy.

—-

The trick to updating AD using power shell is to ensure that you know what object you are updating. In this example I will be using the full ADSI path for each object. There are a lot of other ways to do this, but by using the full ADSI path you can be sure you are only editing the object you want to change…

This script MUST be ran from a Power Shell for AD

—-

####### Begin Power Shell Script #######

$users = Import-csv company.csv

foreach($row in $users)

{

$ADSIPath = $row.DN # This is the path to the object in AD

$user=[ADSI]”LDAP://$ADSIPath”

$Company = $row.Company

if ($Company){ # this is here so that if the company is NULL it will skip that user.

$user.put(“company”, $Company) # the first item is the ADSI attribute we are changing

$user.SetInfo() # this sets the attribute in AD

}

}

####### End Power Shell Script #######

—-

The CSV file looks like this:

DN,Company

“CN=First User Name,OU=Second OU,OU=FirstOU,DC=Company,DC=com”,”ACME Co”

“CN=Second User Name,OU=Second OU,OU=FirstOU,DC=Company,DC=com”,”ACME Co”

You can change most attributes this way. This is a good way to update phone numbers or addresses.
SuperG

Posted in Active Directory, PowerShell • Tags: , Top Of Page

Write a comment

You need to login to post comments!