New PS user having issue with simple AD update

Welcome Forums General PowerShell Q&A New PS user having issue with simple AD update

This topic contains 2 replies, has 2 voices, and was last updated by

4 weeks ago.

  • Author
  • #112355

    Points: 0
    Rank: Member

    Let me preface this post with the fact that I'm extremely green when it comes to PS. (As in, today is my first day using it.) I've been tasked with updating a large number of AD user accounts with charge codes. It was decided that we should use the Department field for this. Unfortunately, the csv that was provided to me comes from an ancient Intranet where the only information that is useful that matched up to AD is the user's employee number and charge code. The csv has the headers of User and Department. I know this should be a simple script, but I'm still getting my feet wet here, and am stumbling around in the dark, as it were. Any insight would be much appreciated.

    $Userdata = Import-Csv C:\empID.csv
    ForEach ($User in $Userdata) {
      $User = $User.User 
      $Department = $Department.Department
      Get-ADUser -Filter "EmployeeNumber -eq '$User'" -Properties * |
      set-AdUser -Add "'$User' -eq 'Department'"  -Properties Department
  • #112367

    Points: 1
    Rank: Member

    So, your first problem is that you're using $User in your ForEach, and then "overwriting" that on line 3. I would rewrite lines 3, 5, and 6 to use something other than $User. E.g., "$EmployeeNumber = $User.user" or something.

    Second, I'm not sure where you're getting $Department.Department from. I suspect you meant $User.Department, since $User represents the current line of the CSV file. Well, it will if you stop overwriting it in line 3.

    You're using -Add incorrectly, and I'm not sure you actually should be using it at all. Department is a single-valued field in AD, and it already exists. And -Add isn't intended to take a comparison.

    Set-ADUser -Department $Department

    That should set the Department attribute for whatever user Get-ADUser spat out.

    • #112394

      Points: 0
      Rank: Member

      Much obliged, sir. I got it ironed out.

You must be logged in to reply to this topic.