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.
Robert Perez-CoronaSystems AdministratorAsked:
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.

Jose Gabriel Ortega CastroEE Solution Guide/Topic Advisor and CEO Faru Bonon ITCommented:
What is the content of your "email-list.txt" ?

You can search the users using either samaccountname or email.
ivan rosaCommented:
Hi Robert,
try the below w/ one user first, once confirmed works. you can add the rest of the users
$users = Get-Content 'C:\temp\email-list.txt'
$targetmembergroup = "group_target_goes_here"
foreach ($user in $users){
    try{
        $ntuser = (Get-ADUser -Filter {userprincipalname -eq $user}).SamAccountName
        Add-ADGroupMember -Identity $targetmembergroup -Members $ntuser -Verbose
        Start-Sleep -s 2
    }catch{
         Write-Host -ForegroundColor Red -BackgroundColor Black  "$($_.Exception.Message)"
         Start-Sleep -s 2        
    }    
}
Write-Host -ForegroundColor Green "Script COmplete!"

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
Robert Perez-CoronaSystems AdministratorAuthor Commented:
Thank you for the help!
Robert Perez-CoronaSystems AdministratorAuthor Commented:
One quick last question: If I were to Remove user users instead of add; can I simply use the Remove-ADGroupMembership cmdlet?
ivan rosaCommented:
You would have to incorporate it with this:


Remove-ADGroupMember -Identity  $targetmembergroup member $ntuser -Confirm $false
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
Powershell

From novice to tech pro — start learning today.