Ying Li at myITforum.com

PowerShell & System Center

One step further - PowerShell script to modify multiple users' property in Active Directory

In my previous script, I showed how we can add or modify the user propertites in Active Directory for single user. Now as a by request script, I will go one step further and try to do the samething for multiple users. The trick is you need to have a csv file ready and the import-csv cmdlet.

I have a sample users.csv file and it looks like this:

DN                                                                                       Telephonenumber
CN=UserA,OU=X,OU=Y,OU=Z,DC=what,DC=ever,DC=com      xxx-yyy-zzz
CN=UserB,OU=A,OU=B,OU=C,DC=what,DC=ever,DC=com     aaa-bbb-ccc
CN=UserC,OU=L,OU=M,OU=N,DC=what,DC=ever,DC=com     lll-mmm-nnn

 

Once you have the csv file ready – you could run the below script against it (You need to have the approriate right to your domain!)

$users = import-csv users.csv
foreach($row in $users)
{
$dn = $row.dn
$user=[ADSI]"LDAP://$dn"
$tel = $row.telephonenumber
$user.put("telephoneNumber", $tel)
$user.SetInfo()
}

You could change the Telephonenumber to EmployeeID or whatever fields you are interested at and it can target as many users as you want. Of course, test it in a small scale first!

Posted: Sep 11 2007, 10:15 AM by yli628 | with 9 comment(s)
Filed under:

Comments

subnetangel said:

I am having trouble with this I am trying to use this to update a custom attribute in AD

This what I have done and it does not work.

$users = import-csv users2.csv

foreach($row in $users)

{

$dn = $row.dn

$user=[ADSI]"LDAP://$dn"

$vip = $row.extensionattribute4

$user.put("extensionAttribute4", $vip)

$user.SetInfo()

}

it gives me an error on theis line:

$user.put("extensionAttribute4", $vip)

please help!!!!!!!!!!

# April 22, 2008 5:13 PM

yli628 said:

I worked with "subnetangel" through email and the issue is with the "DN".

# April 26, 2008 8:09 PM

ITspecialist said:

Hello,

I have a few questions and wish to extend the script you've written.

Is it possible 'logonname' instead of using DN as the UID?

If I wish to change a number of attributes how would this be written?

many thanks

# July 15, 2008 10:23 AM

yli628 said:

Hi,

In order to use LDAP search, we have to use CN here.

What exactly you need to do? In this script, I showed how to change one attribute for multiple users and in my previous script, I showed how to change multiple attributes for one user. You can combine these two to change multiple attributes for multiple users?

Thanks,

Ying

# July 15, 2008 7:31 PM

roryschmitz said:

Hi,

About two months ago I had this working perfectly....not I'm struggling.  I keep getting this error once I run the .ps1 script:

Exception calling "SetInfo" with "0" argument(s): "The attribute syntax specified to the directory service is invalid.

(Exception from HRESULT: 0x8007200B)"

At C:\Documents and Settings\administrator.domain\Desktop\AD Modification Final Step\AD_Modify.ps1:46 char:14

+ $user.SetInfo( <<<< )

Google hasn't been my friend thus far...any ideas?

# July 28, 2008 11:55 AM

roryschmitz said:

I think I found my problem.  I am populating the "manager" attribute and I wasn't using the correct Fully Distinguised Naming convention of: "CN=John Doe,OU=X,OU=Y,OU=Z,DC=what,DC=ever,DC=com"

I had simply put the name as text.  e.g. John Doe

It's too bad PS couldn't tell me which attribute was the culprit.  I overlooked it by mistake...

Nice article though!  Very helpful!

# July 28, 2008 2:17 PM

yli628 said:

Cool, Glad you figure it out. I noticed that when I test my script, the DN has to be exact! When I run into issue, I just test the script line by line to find the "culprit".

Thanks,

Ying

# July 28, 2008 5:48 PM

Turbo01139 said:

$users = import-csv users.csv

foreach($row in $users)

{

$dn = $row.dn

$user=[ADSI]"LDAP://$dn"

$tel = $row.extensionAttribute1

$user.put("extensionAttribute1", $tel)

$user.SetInfo()

}

on line 7 ($user.put...) I became a error

Exception retrieving member "put": "Unknown error (0x80005000)"

At :line:7 char:9

+ $user.put <<<< ("extensionAttribute1", $tel)

Please can you help me

# April 22, 2009 6:44 AM

yli628 said:

Which version of PS you are running?

I will need more information like some sample of your users.csv and your environment?

Thanks,

Ying

# April 22, 2009 8:58 PM