powershell, recursively get AD users excluding certain groups

I need to recursively collect AD users for a list of groups but if I encounter a group that is a nested member that is in my $ExclusionList I do not want to add those to the $RecursiveMembers collection.

I know how to build the recursive collection but how can I add only the users in the non excluded groups to the collection, how do I append the collection ?
J CAsked:
Who is Participating?
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.

Michael B. SmithExchange & Active Directory ExpertCommented:
I wrote this a number of years ago: https://www.essential.exchange/2012/05/04/processing-large-and-embedded-groups-in-powershell/

I could probably write it to run much faster these days, but it works, so I still use it. :-)

If I understand what you want, you would need to add your changes around the line where it says "continue duplicate group".

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
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.