Read CSV add to group Powershell

 

Hi guys,

Today my dear friend Yossi brought me kind of task to add new members to a security group, so I got CSV file with list of Mail attribute, If you look into the script you can see that

I am querying MAIL parameter and retrieve the SamAccountName and then exporting it to CSV

Look at the following script:

 

$File = Get-Content C:\PlaygroundPowershell\EmailAddress_List.csv
Foreach ($a in $File)

{
Get-AdUser -Filter {Mail -eq $a} -Properties Mail | Select SamAccountName |
Export-Csv C:\PlaygroundPowershell\EmailAddress_List.csv  -NoTypeInformation Append

}

Once CSV exported, the second stage is to add the users from the CSV to a security group by the following command:

$Users = Import-Csv C:\PlaygroundPowershell\EmailAddress_List.csv
Add-ADgroupMember -identity "GroupName" -Members $Users.SamAccountName

It is not complicated script, but it was important for me to share it with you.

Big thanks yo Yossi that challenged me!

About Meir Peleg 338 Articles
My name is Meir Peleg, (Born 30/04/1991) live in ISRAEL. Live and breath IT world. Microsoft MVP Windows IT & VMware vExpert.