# aduc powershell

**URL:** <https://forums.powershell.org/t/aduc-powershell/9866>\
**Category:** PowerShell Help\
**Created:** [December 15, 2017, 1:36pm UTC](https://forums.powershell.org/t/aduc-powershell/9866 "2017-12-15T13:36:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrodriguez](https://avatars.discourse-cdn.com/v4/letter/c/5daacb/32.png) [@chrodriguez](https://forums.powershell.org/u/chrodriguez)\
**Post date:** [December 15, 2017, 1:36pm UTC](https://forums.powershell.org/t/aduc-powershell/9866/1 "2017-12-15T13:36:00Z")

</div>

Not sure if this is the right audience, but I’m struggling trying to change the “Last Name” attribute for a bunch of users. I would like to call a csv that I’ve created with usernames and change the last name attribute.

I have this but ONLY need to change last name attribute, please help!

# 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: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=petsmart,DC=com” |  
Set-ADUser -Title $($user.Title) -MobilePhone $($user.MobilePhone) -OfficePhone $($user.OfficePhone) -City $($user.City) -EmailAddress $($user.EmailAddress) -Department $($user.Department) -Office $($user.Office)  
}

---

<div class="post-metadata">

**Author:** ![will-anderson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/will-anderson/32/234_2.png) [@will-anderson](https://forums.powershell.org/u/will-anderson)\
**Post date:** [December 15, 2017, 1:42pm UTC](https://forums.powershell.org/t/aduc-powershell/9866/2 "2017-12-15T13:42:04Z")

</div>

```
Get-ADUser -Identity 'bobadmin' | Set-ADUser -Surname 'lastnamerson'
```

_ **EDIT** _ Sorry. -Surname not -GivenName

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [December 15, 2017, 1:43pm UTC](https://forums.powershell.org/t/aduc-powershell/9866/3 "2017-12-15T13:43:16Z")

</div>

If your CSV contains an Identity column and a GivenName column…

Import-CSV whatever.csv | Set-ADUser

Should work.

---

<div class="post-metadata">

**Author:** ![sanchez](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sanchez](https://forums.powershell.org/u/sanchez)\
**Post date:** [December 15, 2017, 1:46pm UTC](https://forums.powershell.org/t/aduc-powershell/9866/4 "2017-12-15T13:46:53Z")

</div>

What is in your csv file?  
I don’t see anything about surname in your code???

If you had a csv with columns of samaccountname, surname you could just do

```
import-csv C:\blah.csv | % {set-aduser -identity $_.samaccountname -surname $_.surname}
```

---

<div class="post-metadata">

**Author:** ![chrodriguez](https://avatars.discourse-cdn.com/v4/letter/c/5daacb/32.png) [@chrodriguez](https://forums.powershell.org/u/chrodriguez)\
**Post date:** [December 15, 2017, 1:52pm UTC](https://forums.powershell.org/t/aduc-powershell/9866/5 "2017-12-15T13:52:37Z")

</div>

in my csv is just the username

so all I need to do is just edit surname to say: stores  
for a list of users and don’t know exact ps cmd.

---

<div class="post-metadata">

**Author:** ![sanchez](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sanchez](https://forums.powershell.org/u/sanchez)\
**Post date:** [December 15, 2017, 1:55pm UTC](https://forums.powershell.org/t/aduc-powershell/9866/6 "2017-12-15T13:55:03Z")

</div>

You need the surnames in a list somewhere or you are kind of defeating the purpose of using Powershell to automate this.

As noted above, set-aduser -surname is what you want.

---

<div class="post-metadata">

**Author:** ![chrodriguez](https://avatars.discourse-cdn.com/v4/letter/c/5daacb/32.png) [@chrodriguez](https://forums.powershell.org/u/chrodriguez)\
**Post date:** [December 15, 2017, 1:57pm UTC](https://forums.powershell.org/t/aduc-powershell/9866/7 "2017-12-15T13:57:08Z")

</div>

I got it to work, you all rock!!

Thank you

---

<div class="post-metadata">

**Author:** ![sanchez](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sanchez](https://forums.powershell.org/u/sanchez)\
**Post date:** [December 15, 2017, 2:04pm UTC](https://forums.powershell.org/t/aduc-powershell/9866/8 "2017-12-15T14:04:00Z")

</div>

Yep something like

username,surname  
user1,lastname1  
user2,lastname2  
user3,lastname3

Then run:

```
import-csv C:\blah.csv | % {set-aduser -identity $_.username -surname $_.surname}
```

---

<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:37pm UTC](https://forums.powershell.org/t/aduc-powershell/9866/9 "2024-05-16T20:37:00Z")

</div>

Closed on May 16, 2024, 8:37pm UTC


