1
votes

I want to update a set of AD users with a nested powershell command and I need help with that :(

  1. Get all users with a specific attribute (Get-ADUser -Filter 'extensionAttribute1 -like "*"')
  2. Add this users to a specific group (Add-ADGroupMember -Identity "GroupNAME" -Member USERNAME)
  3. Delete the extension attribute (Set-ADUser –Identity USERNAME -Clear "extensionattribute1")

Thanks for your help!

1
What have you tried so far?TobyU
You can see my commands in the (). Problem is I don't know how to nest this.cr1zz

1 Answers

2
votes

Have a look at how to use a result set within a foreach loop like this:

Get-ADUser -Filter 'extensionAttribute1 -like "*"' | foreach {

    Add-ADGroupMember -Identity "GroupNAME" -Members $_.samaccountname
    Set-ADUser –Identity $_.samaccountname -Clear "extensionattribute1"
}