Avatar of Robert Perez-Corona
Robert Perez-Corona
Flag for United States of America asked on

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

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

Avatar of undefined
Last Comment
ivan rosa

8/22/2022 - Mon
Jose Gabriel Ortega Castro

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

You can search the users using either samaccountname or email.
ASKER CERTIFIED SOLUTION
ivan rosa

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Robert Perez-Corona

ASKER
Thank you for the help!
Robert Perez-Corona

ASKER
One quick last question: If I were to Remove user users instead of add; can I simply use the Remove-ADGroupMembership cmdlet?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ivan rosa

You would have to incorporate it with this:


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