foreach question

Welcome Forums General PowerShell Q&A foreach question

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

js
 
Participant
1 month, 4 weeks ago.

  • 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: 1
    Replies: 1529
    Points: 2,587
    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: 483
    Points: 1,151
    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: 1041
    Points: 3,439
    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: 25
      Replies: 678
      Points: 1,629
      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

    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: 1041
      Points: 3,439
      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: 46
    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: 483
    Points: 1,151
    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

    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.

You must be logged in to reply to this topic.