# Update AD User from CSV

**URL:** <https://forums.powershell.org/t/update-ad-user-from-csv/11606>\
**Category:** PowerShell Help\
**Created:** [November 2, 2018, 10:24am UTC](https://forums.powershell.org/t/update-ad-user-from-csv/11606 "2018-11-02T10:24:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![w4gi](https://avatars.discourse-cdn.com/v4/letter/w/dfb087/32.png) [@w4gi](https://forums.powershell.org/u/w4gi)\
**Post date:** [November 2, 2018, 10:24am UTC](https://forums.powershell.org/t/update-ad-user-from-csv/11606/1 "2018-11-02T10:24:37Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![wes-stahler](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wes-stahler/32/227_2.png) [@wes-stahler](https://forums.powershell.org/u/wes-stahler)\
**Post date:** [November 2, 2018, 11:22am UTC](https://forums.powershell.org/t/update-ad-user-from-csv/11606/2 "2018-11-02T11:22:58Z")

</div>

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](https://docs.microsoft.com/en-us/powershell/module/addsadministration/set-aduser?view=win10-ps)

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [November 2, 2018, 5:22pm UTC](https://forums.powershell.org/t/update-ad-user-from-csv/11606/3 "2018-11-02T17:22:47Z")

</div>

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.
> 
> > **[Browse code samples](https://learn.microsoft.com/en-us/samples/browse/?redirectedfrom=TechNet-Gallery)**
> >
> > Get started with Microsoft developer tools and technologies. Explore our samples and discover the things you can build.

> 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
> 
> > **[Browse code samples](https://learn.microsoft.com/en-us/samples/browse/?redirectedfrom=TechNet-Gallery)**
> >
> > Get started with Microsoft developer tools and technologies. Explore our samples and discover the things you can build.

> 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](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
}
```

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [November 3, 2018, 1:38pm UTC](https://forums.powershell.org/t/update-ad-user-from-csv/11606/4 "2018-11-03T13:38:17Z")

</div>

@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

> **[about Quoting Rules - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.2&viewFallbackFrom=powershell-6)**
>
> Describes rules for using single and double quotation marks in PowerShell.

---

<div class="post-metadata">

**Author:** ![w4gi](https://avatars.discourse-cdn.com/v4/letter/w/dfb087/32.png) [@w4gi](https://forums.powershell.org/u/w4gi)\
**Post date:** [November 5, 2018, 5:01am UTC](https://forums.powershell.org/t/update-ad-user-from-csv/11606/5 "2018-11-05T05:01:11Z")

</div>

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

```
}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/update-ad-user-from-csv/11606/6 "2024-05-16T20:33:45Z")

</div>


