Exchange Add multiple users to Distr group based on AD group membership

How can I add members to an distr group in Exchange based on membership of an AD group.

$members = (get-group Consultants).members
Add-distributiongroupmember -identity "Consultants" -members $members

This one fails. I would like this AD group Consultants to be queried each time to verify members and from there add them to the given mail distr group.
Mr WooberAsked:
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.

Marwan OsmanCommented:
Get-ADGroupMember "test11" | select samaccountname | export-csv "C:\users.csv" -NoTypeInformation
Import-CSV "C:\users.csv" | % {  
Add-ADGroupMember -Identity "test12" -Members $_.samaccountname

the above example will add the members of the group test11 to the group test12, run it from active directory powershell after importing the AD module by running import-module activedirectory

I verified it and it works successfully. then if you want to query the script automaticaly, you must add it to task scheduler.

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
Mr WooberAuthor Commented:

It seems fine, If handling only Active Directory groups :)

I want (AD group) members to be added to (Exchange mail distribution list group). I am not able to add to mail distribution list based on samaccountname.

The logic will be that if a user is member of a AD group(consultants) he should be added to Mail Distribution list (consultants)
Try this though littile effort but it works.

Create a Universal Security Group and add all the members to this Security Group.
Now add the Security group in Distribution group.

Hope this helps.
SD-WAN: Making It Work for You

As bandwidth requirements and Internet costs grow, businesses naturally want to manage budgets by reducing reliance on their most expensive connection types. Learn more about how to make SD-WAN work for your business in our on-demand webinar!

Marwan OsmanCommented:
it must works since the exchange distribution group is integrated in active directory, did you try it?
Marwan OsmanCommented:
I just tried it and it is able to add the users to a distribution list group
Mr WooberAuthor Commented:
Marwan Osman, it worked.

I think you might have an "solution" for this one to :)

I would like the members to always be exact the same. I achieve what i asked for on how to add members, but how can I maintain that these members are always the same ?
Marwan OsmanCommented:
for sure it is the same unless an admin add a user to the Distribution group directly so this one will not be synchronized to the AD group, but surely every user added to the AD group will be synchronized to the Distribution group, if you want create a second script which export the users in the distribution group and add them to the AD, and run the 2 scripts on daily basis through the task scheduler :)
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.