We help IT Professionals succeed at work.

Powershell script to add users into a group in Active Directory using their email addresses

Last Modified: 2019-02-23
I am trying to get the script below to Import a list of user emails from a txt files, then provide me with the samaccountname and then inject the samaccount names into an AD distribution list or Sec group.

So far the script generates the Samaccountname. However, I am stuck on the part where the samaccountnames get added to the AD group.

I believe(could be wrong) I can add users to an AD group based on samaccountname but not email addresses. Which is why I am retrieving the samaccountname based on the email to then push them into the AD group.

Get-Content C:\temp\email-list.txt | ForEach {

    Get-ADUser -Filter "mail -eq '$_'" | Select SamAccountName 


Open in new window

Thank you in advance.
Watch Question

Jose Gabriel Ortega CastroCEO Faru Bonon IT&Agency /Top-Rated Freelancer (Upwork)/Photographer
Awarded 2018
Distinguished Expert 2018

What is the content of your "email-list.txt" ?

You can search the users using either samaccountname or email.
This one is on us!
(Get your first solution completely free - no credit card required)
Robert Perez-CoronaSystems Administrator


Thank you for the help!
Robert Perez-CoronaSystems Administrator


One quick last question: If I were to Remove user users instead of add; can I simply use the Remove-ADGroupMembership cmdlet?
You would have to incorporate it with this:

Remove-ADGroupMember -Identity  $targetmembergroup member $ntuser -Confirm $false

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.