powershell script to add only group with specific name


i have this comand line to copy group of user1 and add user 2 to this group.
i use a csv file with a list to group to not copy/exclude from this script:

$exclude = Get-Content groupexclusion.txt
            Get-ADPrincipalGroupMembership -Identity $user1 | ? {$exclude -notcontains $_.name} | % {Add-ADPrincipalGroupMembership -Identity $user2 -MemberOf $_}

what i need now is to continue to use this script and add this :

copy only group that name begin with "GR"  "GK"  AND CONTINUE TO USE the csv file extension, SO THE SCRIPT will select all group begin with this char that user1 is member, verify that this group is not in exclusion csv file and add it to user 2.

thank for help
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Just add an extra condition on the where statement.

$exclude = Get-Content groupexclusion.txt
Get-ADPrincipalGroupMembership -Identity $user1 | ? {$exclude -notcontains $_.name -and ($_.name -like "GR*" -or $_.name -like "GK*")} | % {Add-ADPrincipalGroupMembership -Identity $user2 -MemberOf $_}

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cawasakiAuthor Commented:

Thanks for this

Are this is work with your code:

On the csv exclusion file are the group named gkgroup1. So it begin with gk, are this group will be excluded or not? Because we have add on code to copy all group namébegin with gk?.

If the $exclusion array contains a group called gkgroup1 then no it will not be added. This is because there is a and condition. The where command is expecting either a true result. So...

$exclude -notcontains $_.name -and ($_.name -like "GR*" -or $_.name -like "GK*")

$exclude -notcontains $_.name will return false.
$_.name -like "GR*" -or $_.name -like "GK*" will return true.

Where will be only match if it returns true -and true. So the where conditions will fail so the group will not be added if gkgroup1 is in $exclusions.
cawasakiAuthor Commented:
thanks its work
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.