aduc powershell

This topic contains 7 replies, has 4 voices, and was last updated by  Jon 4 months ago.

  • Author
    Posts
  • #89335

    chrodriguez1
    Participant

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

  • #89345

    Will Anderson
    Keymaster
    Get-ADUser -Identity 'bobadmin' | Set-ADUser -Surname 'lastnamerson' 

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

  • #89348

    Don Jones
    Keymaster

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

    Import-CSV whatever.csv | Set-ADUser

    Should work.

  • #89351

    Jon
    Participant

    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}
    • #89357

      chrodriguez1
      Participant

      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.

    • #89359

      Jon
      Participant

      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.

    • #89360

      chrodriguez1
      Participant

      I got it to work, you all rock!!

      Thank you

  • #89365

    Jon
    Participant

    Yep something like

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

    Then run:

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

You must be logged in to reply to this topic.