Monday, July 11, 2011

Search AD and Add Employee ID–PowerShell Script

Script #1

Here is a simple PowerShell script which you can use to add Employee ID in a user object. This script requires PowerShell Active Directory module.

Note: I am not searching AD for the user account.  My assumption is the user account already exist in AD. 

image

Input file – Input file (Users.csv) contains user names and employeeID in the following format:

image

You can download this script from the following locations:

www.sivarajan.com - http://www.sivarajan.com/scripts/Add_EMpID.txt

Microsoft TechNet Gallery - http://gallery.technet.microsoft.com/scriptcenter/e9bafc1a-b5b1-4663-8e25-b0d0ea28c2b2

Script #2 -  Search AD using email address and update the employee ID value.

image

Input file (users.csv) contains email address and employee ID in the following format:

image

Download - http://www.sivarajan.com/scripts/Search_AD_EmployeeID.txt

Microsoft TechNet Gallery - http://gallery.technet.microsoft.com/scriptcenter/87a2a2f3-f590-4bdf-911b-da4df53f1f11

 


More scripts - http://portal.sivarajan.com/search?q=script+powershell&max-results=20


27 comments:

How can this be modified to allow multiple attributes to be changed. e.g. telephoneNumber to be changed along with employeeNumber

Update the input file with telephone number in the following format:

UserName,EmployeeID,TelNumber
User1,12345,17131112222

And update the code with the correct attribute - http://portal.sivarajan.com/2010/07/aduc-and-ldap-reference-sheet.html

Fax facsimileTelephoneNumber
Home homePhone
Notes info
IP phone ipPhone
Mobile mobile

Here is the updated script:

Import-module ActiveDirectory
Import-CSV "C:\Scripts\Users.csv" | % {
$User = $_.UserName
$ID = $_.EmployeeID
TelN = $_. TelNumber
Set-ADUser $User -employeeID $ID – homePhone TelN
}

Thanks Santosh ! unfortunately I still have trouble

problem appears to be with the attribute "telephoneNumber" and "ipPhone". When I change the script to modify these attributes with "hometelephone" and "mobile" they work just fine.

Script is
-------

Import-module ActiveDirectory
Import-CSV "C:\Scripts\test.csv" | % {
$User = $_.UserName
$ID = $_.empNumber
$TelN = $_.telNumber
$IP = $_.ipP
#Set-ADUser $User -employeeNumber $ID -telephoneNumber $TelN -ipPhone $IP
}

CSV file is
-----------


UserName,empNumber,telNumber,ipP
ABC.XYZ,8888800,+445555555501,5555501



Error is
------------

Set-ADUser : A parameter cannot be found that matches parameter name 'ipPhone'.
At C:\scripts\modify2.ps1:7 char:46
+ Set-ADUser $User -employeeNumber $ID -ipPhone <<<< $IP
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser


Any suggestions ??

The Set-ADUser cmldet doesn’t have an alias for the AD ipPhone attribute. You can see all available alias in the http://technet.microsoft.com/en-us/library/ee617215.aspx link.

However, you can LDAP reference and –instance parameter to set these values.

Here is an example:

$UserN = Get-ADUser Testuser1 -Properties ipPhone
$UserN.ipPhone = "111.222.333.444"
Set-ADUser -instance $UserN

Santhosh, thank you for this article. I am new to PowerShell and have this question:

We already have EmployeeID attribute populated but it does not show up in the property page of a user in ADUC. How can I get that attribute to appear in ADUC?

Thanks so much for your help.

By default, it won’t show up in ADUC. You need to customize the display. Mike has a blog on this procedure -

http://adisfun.blogspot.com/2009/05/add-employee-id-field-aduc.html

Thank you. I was hoping to add it as a display on a tab in ADUC, as we have 100s of people using ADUC. Don't want to call up a script everytime they use it.

Thanks though!!!

This is a onetime change but if you thinking about a Tab or item in the Tab, you need to modify the GUI (Display Specifiers)…not that easy ;) Here are some info..

http://msdn.microsoft.com/en-us/library/ms676902.aspx

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

hello, I need to edit multiple attributes at the same time:
employeeid, EmployeeType, employeenumber, codepage

Hi can one help me to update Employee id and Department id for AD users

Set-ExecutionPolicy RemoteSigned
Import-module ActiveDirectory
Import-CSV "c:\users.csv" | % {
$User = $_.UserName
$ID = $_.EmployeeID
$userobj=([adsisearcher]"samaccountname=$User").FindOne().GetDirectoryEntry()
$userobj.EmployeeID = $ID
$userobj.CommitChanges()
}

Mahesh,

You can update any value using this method/script. Just use the correct attribute name. Here is a reference link -

http://portal.sivarajan.com/2010/07/aduc-and-ldap-reference-sheet.html

ALFONSO MEDICIS,

Same answer!

You can update any value using this method/script. Just use the correct attribute name. Here is a reference link -

http://portal.sivarajan.com/2010/07/aduc-and-ldap-reference-sheet.html

hi, I'm new to poweshell commands. I try to update my employee numbers for more than 1000 employees. I created file as below :-
Import-module ActiveDirectory
Import-CSV "c:\temp\users.csv" | % {
$User = $_.UserName
$ID = $_.empNumber
Set-ADUser $User -employeeID
}
Saved this file as .ps1, when I tried to run it I will get below error :-
Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is
null. Supply a non-null argument and try the command again.
At C:\TEMP\UpdateEmployeeID.ps1:5 char:11
+ Set-ADUser <<<< $User -employeeID
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingV
alidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
eDirectory.Management.Commands.SetADUser

how can I rectify this error. if we want to follow other steps then please update me on patilshirishr@gmail.com

Hi Siva,
Your blog has been really helpful, keep up the great work

Hi Siva,
How can I use your script to edit the Office field in an AD Contact rather than an AD User?

Never mind. I just changed the Set-ADUser command to Set-Contact and ran the script from our Exchange Server so that it used the Exchange Server commands. Worked Great!!

Hello, I know this is an old post. but I am getting an error message (Set-ADUser : Cannot find an object with identity "username" under: 'dc=domain.dc=com. also, confirmed that the user account does exist.
any ideas?
thanks

Dear Santosh,

thanks for your valuable support for so many system administrators,i have always been a fan of you,have read lots of troubleshooting tips and articles written by you,

I want to add assistant field for bulk users ,so in this case how will be script modified,i have all the user account with assistant details.

You can. Use the correct attribute. I have listed all the attribute detail here - http://portal.sivarajan.com/2010/07/aduc-and-ldap-reference-sheet.html

Do you have this user? did you verify this?

This comment has been removed by the author.

I too am getting this error message. I ran this on a single user and wondered if it has to do with the fact that the user is in different container?

I too am getting this error message. I ran this on a single user and wondered if it has to do with the fact that the user is in different container?

I need to update employee number for morethan 200 employees with the help of their email ids. Requesting you to please provide the script and csv file format

Post a Comment

Popular Posts

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More