A while back, I had a user that had to be added to a large number (150+) of active directory groups. Rather than doing so manually, I spent a little time looking up how to automate it, and created a script to help me do it. Here’s how I constructed it:
First, I needed to define my groups. That command starts out with a dollar sign, calling what follows out as a variable. I called the variable Groups, and started adding my groups as part of an array – which is defined by @(ARRAY GOES HERE)
– as follows:
$Groups = @("Group001";
"Group002";
...
"GroupN";)
Next, I added a foreach-loop, stating that I wanted a named user added as a member of each – using the Add-ADGroupMember
command – of the groups. Here’s what that looked like:
ForEach ($Group in $Groups){
Add-ADGroupMember -Identity $Group -Members "USERNAME"
}
Here’s what the complete script looked like:
$Groups = @("Group001";
"Group002";
...
"GroupN";)
ForEach ($Group in $Groups){
Add-ADGroupMember -Identity $Group -Members "USERNAME"
}
By posting a comment, you consent to our collecting the information you enter. See privacy policy for more information.