Update AD Attributes using Powershell.

I was at a customer the other day installing their new helpdesk portal. When we came to the step to import the users from Active Diretory there was almost no info on the user accounts. Only a first name and last name.

We wanted to be able to Group the users by city and we neded their phone numbers. The easiest way to get this information was from the HR department and then build a CSV file that we used with Powershell to import the information to Active Directory.

To help me edit the csv file that I got from the HR department I used the new feature in Excel 2013 called Flash Fill. You can read more about Flash Fill in this Office Blog http://blogs.office.com/2012/08/09/flash-fill/

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#------------------------------------------------------------------------------------------
# Script created by Chris Steding
#
# This script will update AD attributes for users imported from csv file
# csv file format:
# SamAccountName,Title,MobilePhone,OfficePhone,city,EmailAddress,Department,Office
#
# Remember to edit the -SearchBase to the correct domain name.
#-------------------------------------------------------------------------------------------
 
# Import AD Module
Import-Module ActiveDirectory
 
write-Host 'Starting to update AD Attributes.......' -NoNewline -ForegroundColor Yellow
# Import CSV into variable $users
 
$users = Import-Csv -Path C:\temp\users.csv
# Loop through CSV and update users if the exist in CVS file
 
foreach ($user in $users) {
#Search in specified OU and Update existing attributes
Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" -Properties * -SearchBase "DC=Compit,DC=local" |
Set-ADUser -Title $($user.Title) -MobilePhone $($user.MobilePhone) -OfficePhone $($user.OfficePhone) -City $($user.City) -EmailAddress $($user.EmailAddress) -Department $($user.Department) -Office $($user.Office)
}
 
Write-Host 'done!' -ForegroundColor Green

Download the script and a csv:
http://www.compit.se/download/script/Update-ADattributes.zip

Have a nice day // Chris

This entry was posted in Active Directory, Powershell, Scripting and tagged , . Bookmark the permalink.

21 Responses to Update AD Attributes using Powershell.

  1. David Leavitt says:

    Thanks for this script. Saved me lots of time with AD modifications.

  2. Shyam says:

    Thanks, It’s works perfectly

  3. Robert Price says:

    So the script works great on a single account (tested on user in csv file) but when doing it with the original csv file it does not update the info. Any suggestions.

  4. Chris says:

    Hi Robert

    Do you get any error message?

    BR Chris

  5. Robert Price says:

    No error message running it in ISE it runs through the 6 or 7 accounts (in debug mode) and shows it’s done, running as the .ps1 it the same thing (each iteration) but nothing get’s updated. Here are the modifications to the script:
    Import AD Module
    # Import-Module ActiveDirectory

    write-Host ‘Starting to update AD Attributes…….’ -NoNewline -ForegroundColor Black
    # Import CSV into variable $users

    $users = Import-Csv -Path C:\adupdate\OneLastTestUpdate.csv
    # Loop through CSV and update users if the exist in CVS file

    foreach ($user in $users){
    #Search in specified OU and Update existing attributes
    {
    Get-ADUser -SearchBase “DC=XXX,DC=XXX” -Filter “SamAccountName -eq ‘$($user.SamAccountName)'” |
    Set-ADUser -Replace -Title $($user.Title) -StreetAddress $($user.StreetAddress) -City $($user.City) -State $($user.State) -PostalCode $($user.PostalCode) -OfficePhone $($user.OfficePhone) -MobilePhone $($user.MobilePhone) -Office $($user.Office) -Company $($user.Company) -Department $($user.Department)
    }}

    Write-Host ‘done!’ -ForegroundColor Green

  6. Robert Price says:

    Bump

  7. Robert Price says:

    Updated email address.

  8. Shakeel Shahid says:

    Dear Sir,

    I am facing some issues in an on-going project.

    I am importing users in Active Directory from CSV file and after this i need to replace values of some custom attributes of all imported users and the Values needs to be imported from CSV file automatically.

    Please send me your contact details so that i can further discuss with you on this issue.

    Regards,
    Shakeel Shahid.

  9. Abhijit says:

    I got bellow error.

    At C:\GITPLSignature_NotUsed\AddandUpdatetoAD\Update\Update-ADattributes-from-CSV .PS1:23 char:2
    + Get-ADUser -Filter “SamAccountName -eq ‘$($user.samaccountname)'” -Properties * …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

  10. Bearshark says:

    As advertised. I wanted the ‘mail’ attribute populated for users who didn’t have one. Worked against users that already had the attrib populated too.

  11. Carl says:

    cannot seem to add 2 attributes
    1 telephoneNumber
    2 facsimileTelephoneNumber

    can export these fields though but not import back into AD
    any suggestions?

  12. Dipak Patil says:

    Thank you Dear.

    You saved my lot of time.

  13. Abhi Ramana Bikmal says:

    This code is working fine. But I was struck with one issue. Active Directory shows only 1999 user records (rest users are invisible). I was importing around 10,000 users from csv file to Active Directory. But the above code would update only if the users are present in first 1999 records, What if the user record is present in invisible records, Kindly share your suggestion on this?

  14. Mark Mossop says:

    Abhi Ramana Bikmal -> I know that AD has a default filter view of 2000 entries per folder – does it help if you you open “Active Directory Users and Computers” and go to View, Filter Options and change the setting of 2000 to lets say 15000?

  15. Grant says:

    Very useful, thanks very much.
    I’ve edited it for my own needs which was just to add a unique employeeID to 500+ AD user.

    thanks

  16. Thuan says:

    Dear All,

    I am having a problem with modifing the Manager attribute in our AD with a new format BIS10045 that isn’t the Name format as Microsoft required.

    Is there anyone that have got same my issue, please? Could you please give me an advice?
    Thank you so much.
    Best regards,
    Thuan

  17. IT says:

    Hello geek,

    quick question:

    I have a csv file looking like this:

    samAccountName,MeetingNumber
    user01,2.N6:1
    user02,1.39:1
    user03,1.TL2:2
    user04,1.E13:1

    how to use this script? could just do a copy paste and add my info i am very confuse
    if possible ignore the search base option.
    ///////////////
    thanks

  18. Jeremy J says:

    Can I get this work to add text to an ExtensionAttribute?

  19. Michael says:

    Thank you very much for such great job and saved me lot’s of time.
    THANK YOU.
    Michael

  20. Takahashi says:

    Thank you! Worked like a charm!!

  21. Thiyagu Mani says:

    I tried this command. Command success but not updated in AD. In my scenario we have 1 Primary domain and 4 child domain. Pls help how to wrote a script.

Leave a Reply

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