Author Posts

December 15, 2017 at 7:06 pm

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

December 15, 2017 at 7:12 pm

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

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

December 15, 2017 at 7:13 pm

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

Import-CSV whatever.csv | Set-ADUser

Should work.

December 15, 2017 at 7:16 pm

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}

December 15, 2017 at 7:22 pm

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.

December 15, 2017 at 7:25 pm

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.

December 15, 2017 at 7:27 pm

I got it to work, you all rock!!

Thank you

December 15, 2017 at 7:34 pm

Yep something like

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

Then run:

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