Welcome Forums General PowerShell Q&A foreach question

  • This topic has 10 replies, 6 voices, and was last updated 7 months ago by
    js
    Participant
    .
Viewing 8 reply threads
  • Author
    Posts
    • #166930
      Participant
      Topics: 7
      Replies: 9
      Points: 70
      Rank: Member

      Hi guys,

      I would like to change a customattribute for all users in active directory, but script below is not working, what do i do wrong?

       

      $users = get-aduser -Filter * | select samaccountname
      
      foreach ($user in $users)
      {
      Set-AdUser -Identity $user -Replace @{ customAttribute1="something" }
      }

       

    • #166942
      Participant
      Topics: 2
      Replies: 1752
      Points: 3,621
      Helping Hand
      Rank: Community Hero

      but script below is not working, what do i do wrong?

      That’s quite vague. How do you know it is not working? πŸ˜‰ What’s the situation before? What happens when you onvoke this code? What’s the situation after?

    • #166951
      Participant
      Topics: 2
      Replies: 512
      Points: 1,309
      Helping Hand
      Rank: Community Hero

      You need to use Select-Object -ExpandProperty SamAccountName; select samaccountname (or more properly Select-Object -Property SamAccountName if you use the full-form syntax) like you have there will get you the one property attached to an object, rather than just the value of the property — which is what Set-ADUser will need.

    • #166990
      Senior Moderator
      Topics: 8
      Replies: 1160
      Points: 4,056
      Helping Hand
      Rank: Community Hero

      A one liner without foreach

      Get-ADUser -Filter * -Properties customAttribute1 | Set-AdUser -Identity $user -Replace @{ customAttribute1="something" }
      
      • #167353
        js
        Participant
        Topics: 28
        Replies: 751
        Points: 2,096
        Helping Hand
        Rank: Community Hero

        That one-liner should work without the “-Identity $user” after “Set-Aduser”.

    • #167185
      Participant
      Topics: 7
      Replies: 9
      Points: 70
      Rank: Member

      A one liner without foreach

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      Get-ADUser Filter * Properties customAttribute1 | Set-AdUser Identity $user Replace @{ customAttribute1=“something” }
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      This is not helpfull because i need to do it for all the users not per user.

      • #167191
        Senior Moderator
        Topics: 8
        Replies: 1160
        Points: 4,056
        Helping Hand
        Rank: Community Hero

        Set-ADUser here will be executed for each user from Get-ADUser cmdelt. What will be the value for customattribute ?

    • #167188
      Participant
      Topics: 7
      Replies: 9
      Points: 70
      Rank: Member

      You need to use Select-Object -ExpandProperty SamAccountName; select samaccountname (or more properly Select-Object -Property SamAccountName if you use the full-form syntax) like you have there will get you the one property attached to an object, rather than just the value of the property β€” which is what Set-ADUser will need.

      Hi Joel, i think i do understand what you tell mee but i don’t know exaiclly what you mean. I replaced the code with

      Select-Object -Property SamAccountName

      But i still receive the same error message:

      Cannot bind parameter Identity

       

      Any help for me?

       

       

       

    • #167215
      Participant
      Topics: 1
      Replies: 2
      Points: 48
      Rank: Member

      In your loop the way you have it, you can use the propertyname.

       

      $users = Get-ADUser -Filter *
      
      foreach ($user in $users)
      {
        Set-AdUser -Identity $user.samAccountName -Replace @{ customAttribute1="something" }
      }
    • #167239
      Participant
      Topics: 2
      Replies: 512
      Points: 1,309
      Helping Hand
      Rank: Community Hero

      Yep. Or use -ExpandProperty (andΒ not -Property) like I mentioned in the initial one. πŸ™‚

    • #167323
      Participant
      Topics: 7
      Replies: 9
      Points: 70
      Rank: Member

      You need to use Select-Object -ExpandProperty SamAccountName; select samaccountname (or more properly Select-Object -Property SamAccountName if you use the full-form syntax) like you have there will get you the one property attached to an object, rather than just the value of the property β€” which is what Set-ADUser will need.

      Hi Joel, i think i do understand what you tell mee but i don’t know exaiclly what you mean. I replaced the code with

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      Select-Object Property SamAccountName
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      But i still receive the same error message:

      Cannot bind parameter Identity

      Any help for me?

      Thanks Dan! This is the way go, script working.

Viewing 8 reply threads
  • The topic ‘foreach question’ is closed to new replies.