aduc powershell

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)

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

EDIT Sorry. -Surname not -GivenName

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

Import-CSV whatever.csv | Set-ADUser

Should work.

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}

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.

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.

I got it to work, you all rock!!

Thank you

Yep something like


Then run:

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