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!

Leave a Comment

Your email address will not be published.

In the news
Load More