Update AD User from CSV

Welcome Forums General PowerShell Q&A Update AD User from CSV

This topic contains 4 replies, has 4 voices, and was last updated by

 
Participant
1 month, 1 week ago.

  • Author
    Posts
  • #121285

    Participant
    Points: 14
    Rank: Member

    I have to update all User in my AD with certain values from a csv.

    So I wrote a little script, which should do the job for me.

    $WorkObjects =Import-Csv -Encoding UTF8 -Path C:\Temp\signatur.csv -Delimiter ";"
    
    $WorkObjects.Count
    
    foreach ($WorkObject in $WorkObjects)
    
    {
    
    $user=$WorkObject.displayName
    
    $dep=$WorkObject.department
    
    $tit=$WorkObject.title
    
    $l=$WorkObject.l
    
    $street=$WorkObject.StreetAddress
    
    $st=$WorkObject.st
    
    Write-host "$user"
    
    Get-ADUser-Filter 'DisplayName -like "$user"'|Set-ADUser-Title "$tit"-l "$l"-StreetAddress "$street"-State "$st"-Department "$dep"
    
    write-host "Press Enter"
    
    $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    
    }

    I testet it with:

    Write-Host "Get-ADUser" "-Filter" "DisplayName" "-like" $user "| Set-ADUser" "-department" $dep "-title" $tit "-l" $l "-StreetAddress" $street "-State" $st
    I got all the right values from my csv. And when I try to write the "Get-ADUser | Set-ADUser" manually everything works like it should. Does anybody see, what am I missing here?
    Thank you so much!
  • #121312

    Moderator
    Points: 172
    Team MemberHelping Hand
    Rank: Participant

    You state "everything works like it should". I take it that it is not working. Do you get an error?
    I would look at creating a hashtable and using it to make the changes.

    Checkout https://docs.microsoft.com/en-us/powershell/module/addsadministration/set-aduser?view=win10-ps

  • #121438

    Participant
    Points: 316
    Helping Hand
    Rank: Contributor

    The are many articles and pre-built script that you can use as is or tweak as needed for your use case. No reason to start from scratch, unless of course this is a homework assignment or general edification effort. Even in the latter case, learning from others work is paramount for clarity of direction, even if you later come up with a better option / solution.

    Update Active Directory Users in Bulk from CSV

    PowerShell V2 script to update Active Directory users from a CSV file. Only specified fields in the CSV that are not missing update the users. The value "" flags to clear the attribute. Attributes are not updated if the value in the CSV matches the existing value in AD.

    https://gallery.technet.microsoft.com/scriptcenter/Update-Active-Directory-cd5c5513

    Update Active Directory User attributes from CSV

    This script will feed data from CSV file to Active directory user attributes.I have included two scripts one will overwrite the existing data & other will only write if existing attributes are blank.Prerequisites:- quest management tools should be installed.CSV format:- This is a

    https://gallery.technet.microsoft.com/scriptcenter/Feeding-data-to-Active-0227d15c

    PowerShell: Bulk create AD Users from CSV file

    https://social.technet.microsoft.com/wiki/contents/articles/24541.powershell-bulk-create-ad-users-from-csv-file.aspx

    If all your values are already in the file, then just use those directly. No real need for the Get-ADUser.
    It could as simple as doing something link this (splatting being used just for better readability)...

    $Users = Import-CSV c:\script\userattributes.csv
    
    foreach ($User in $Users)
    {
        $ADUserParams = @{
            displayName   = $User.displayname
            department    = $User.department
            title         = $User.title
            l             = $User.l
            StreetAddress = $User.streetaddress
            st            = $User.st
        }
        Set-ADUser @ADUserParams
    }
    
  • #121537

    Participant
    Points: 832
    Helping Hand
    Rank: Major Contributor

    @W4gi28, Your filter wont work as you have wrapped them in single quotes. When using quotes,variable expansion works only inside double quotes.

    #current code
    Get-ADUser -Filter 'DisplayName -like "$user"'
    
    #correct it like below
    Get-ADUser -Filter "DisplayName -like '$user'"
    

    And see more here at
    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6

  • #121830

    Participant
    Points: 14
    Rank: Member

    Thank you all very much for your help!

    @kvprasoon Thank you for the hint, I did the changes according to you.

    That is the code which was working for me now.

    $WorkObjects =Import-Csv -Encoding UTF8 -Path C:\Temp\signatur.csv -Delimiter ";"
    
    $WorkObjects.Count
    
    foreach ($WorkObject in $WorkObjects)
    
    {
    
     $user=$WorkObject.displayName
     $dep=$WorkObject.department
     $tit=$WorkObject.title
     $l=$WorkObject.l
     $street=$WorkObject.StreetAddress
     $st=$WorkObject.st
    
     Write-host "$user"
    
     Get-ADUser-Filter "DisplayName -like '$user'"|Set-ADUser -Replace@{title="$tit";l="$l";streetAddress="$street";department="$dep";st="$st"}
     
     write-host"Press Enter"
    $x=$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }

You must be logged in to reply to this topic.