Update AD User from CSV

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!

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

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 “<delete>” flags to clear the attribute. Attributes are not updated if the value in the CSV matches the existing value in AD.

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

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
}

@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

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")
}