Bulk update user properties

Welcome Forums General PowerShell Q&A Bulk update user properties

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

 
Participant
3 months, 2 weeks ago.

  • Author
    Posts
  • #103570

    Participant
    Points: 0
    Rank: Member

    I have this csv that has 3 properties and is importing correctly but when I do the Set-ADUser in the ForEach, I get error

    Set-ADUser : A parameter cannot be found that matches parameter name 'mail'.

    Here is my script:

    $users = Import-Csv -Path .\SourceAttributes.csv
    
    # Loop through CSV and update users if the exist in CSV file            
        foreach ($user in $users) {                       
     Get-ADUser -Filter "SamAccountName -eq '$($user.sAMAccountName)'" -Properties * |            
      Set-ADUser -Manager $($user."Primary Contact") -mail $($user."Technical Contact") -WhatIf            
    }
  • #103576

    Participant
    Points: 8
    Rank: Member

    If you review the help for Set-ADUser you'll see that the parameter is named "-EmailAddress".
    BTW: When you have the sAMAccountName in your csv file you don't need to get the account with Get-ADUser first. You can use right away Set-ADUser with the parameter -Identity wich takes the sAMAccountName.

  • #103723

    Participant
    Points: 0
    Rank: Member

    Thanks.

    revised script:

        foreach ($user in $users) {                       
     Get-ADUser -Identity '$($user.sAMAccountName)' -Properties * | Set-ADUser -Replace -Manager $($user."Primary Contact") -emailaddress $($user."Technical Contact")           
    }

    new error:

    Set-ADUser : Cannot bind parameter 'Replace'. Cannot convert the "-Manager" value of type "System.String" to type "System.Collections.Hashtable".
  • #103726

    Participant
    Points: 19
    Rank: Member

    The -Replace parameter takes a hashtable of one or more values to replace. If you want to set values individually, use other parameters. To replace a bunch at once, use -Replace. Not all AD properties have explicit parameters laid out for you to use with the cmdlet. It is mostly for those that the -Add, -Remove, and -Replace parameters exist; to update those parameters that aren't accessible through other parameters.

    It'll still work for regular ones, though, and it's a bit neater than specifying every parameter.

    foreach ($user in $users) {                       
        Set-ADUser -Identity '$($user.sAMAccountName)' -Replace @{
            Manager = $user."Primary Contact"
            EmailAddress = $user."Technical Contact"
        }
    }
    • #103739

      Participant
      Points: 0
      Rank: Member

      Joel,

      I had to remove a single quote mark from around part of the loop. So, with the revised script:

      foreach ($user in $users) {                       
          Set-ADUser -Identity $($user.sAMAccountName) -Replace @{
              Manager = $user."Primary Contact"
              EmailAddress = $user."Technical Contact"
          }
      }

      I get a new error:

      Set-ADUser : The specified directory service attribute or value does not exist
      Parameter name: EmailAddress

      ...yet, I have verified with Get-ADUser that the samAccountName for each in my csv exists.

      Does it matter that a current value exists in AD for any given samAccountName? It doesn't matter that this would get replaced by my values in the .csv.

  • #103742

    Participant
    Points: 19
    Rank: Member

    Nah, that's kinda what you're after.

    Lemme see... Okay, the issue is probably that the property names differ slightly depending on your method. Try applying the email using 'mail' as the property name instead. (Or just pull that one out of the hashtable and apply it with the -EmailAddress parameter instead.

    • #103745

      Participant
      Points: 0
      Rank: Member

      Not following you exactly I'm afraid, but I tried this and got a different error.

      foreach ($user in $users) {                       
       Set-ADUser -Identity $($user.sAMAccountName) emailAddress = $user."Technical Contact"
      }

      ERROR

      Set-ADUser : A positional parameter cannot be found that accepts argument 'emailAddress'.

      OR:

      foreach ($user in $users) {                       
       Set-ADUser -Identity $($user.sAMAccountName) mail = $user."Technical Contact"
      }
      

      ERROR

      Set-ADUser : A positional parameter cannot be found that accepts argument 'mail'.

      You don't think it's my csv file being in a weird .csv format? I save mine in Excel as a MS-DOS csv type

  • #103783
    Jon

    Participant
    Points: 2
    Rank: Member

    You need "-emailaddress" as per the documentation

    https://docs.microsoft.com/en-us/powershell/module/addsadministration/set-aduser?view=win10-ps

    You are also over complicating things with having to worry about substrings, I would simplify it a bit. In your CSV I would have a column with samaccountname, mail, manager etc then...

    import-csv C:\attributes.csv | % {set-aduser -identity $_.samaccountname -emailaddress $_.mail -manager $_.manager }
    • #103807

      Participant
      Points: 0
      Rank: Member

      Thanks Jon, That's exactly what's in my .csv so will try your simplified one-liner.

  • #103828

    Participant
    Points: 0
    Rank: Member

    I feel like I am getting close to this and the help file for Set-ADUser wasn't helping completely. It doesn't address the use of quotes when using the -Replace Parameter. When i used them, I was able to write values without error.

    Anyway, I have this new code:

    foreach ($mgr in $mgrs) {                       
        Set-ADUser -Identity $($mgr.sAMAccountName) -Replace @{
           otherMailbox = '$mgr."PrimaryContact"'
        }
    }

    ...and it does write to the intended attribute (otherMailbox) but it isn't just the mail address I want from my .csv. It's many different things depending on how I arrange the quotes.

    I've tried the following:

    $mgr."PrimaryContact" ERROR: Set-ADUser : replace At line:2 char:5...)
    "'$mgr'.PrimaryContact" SUCCES WRITE but {'@{sAMAccountName=svcacctjfw; PrimaryContact=}'.PrimaryContact}
    $mgr.PrimaryContact ERROR: Set-ADUser : replace At line:2 char:5...)

    ...before throwing my hands up in the air!

    I just want the next string (mgr.PrimaryContact) in the pipeline (that I verified exists) to populate the otherMailbox attribute.

  • #103840

    Participant
    Points: 19
    Rank: Member

    You're very close. Your last issue there is that single quotes do not expand variables in strings, period. In this instance, you could simply drop all quotes from the expression. Or, you can use double quotes (but you'd need to be careful because of how PS handles variable properties in strings — it doesn't, essentially). Or you could use a format string. So... options:

    # Option 1
    Set-ADUser -Identity $mgr.sAMAccountName -Replace @{
       otherMailbox = $mgr.PrimaryContact
    }
    # Option 2
    Set-ADUser -Identity $mgr.sAMAccountName -Replace @{
       otherMailbox = "$($mgr.PrimaryContact)"
    }
    # Option 3
    Set-ADUser -Identity $mgr.sAMAccountName -Replace @{
       otherMailbox = '{0}' -f $mgr.PrimaryContact
    }

    In this case, because the property name has no spaces, actually having quotation marks around the property name itself is unnecessary.

    • #103852

      Participant
      Points: 0
      Rank: Member

      Joel, thanks for all of the examples

The topic ‘Bulk update user properties’ is closed to new replies.