cr1zz
cr1zz

Reputation: 577

PowerShell: Nested powershell command to add users to group

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!

Upvotes: 1

Views: 516

Answers (1)

TobyU
TobyU

Reputation: 3908

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"
}

Upvotes: 2

Related Questions