PowerShell Add Users To AD Group From CSV via Email
In this article, we will discuss adding users to the AD group from CSV using their email addresses. This script queries the user’s email address instead of the user’s principal name, which is very helpful in environments where the user’s email address doesn’t match the UPN.
Prepare CSV with Users’ Email Address
The first step is to create a CSV with the user’s email address and correct header. Open Notepad and enter “EmailAddress” in the first line, CSV’s header.
EmailAddress
User1EmailAddress
User2EmailAddress
I have created a new folder where this CSV file will be saved. “C:\PSData” and this location will be used in the PowerShell script. Please feel free to create the same folder structure or use a different location and modify the CSV location in the PowerShell script
Get AD Security Group Name From Active Directory
Open the Active Directory Users and Computers console, find the security group you want to import users to and copy the group name.
PowerShell Script to Incorporate Users from CSV into AD Security Group
# Import AD Module Import-Module ActiveDirectory # Please replace 'ADGroupName' below with AD Security group $GroupName = 'ADGroupName' # Please enter correct CSV location, this PowerShell Script is using "C:\PSData\UserList.csv" $Users = Import-Csv "C:\PSData\UserList.csv" Import-CSV -path "C:\PSData\UserList.csv" | Foreach-Object { $aduser = Get-ADUser -Filter "EmailAddress -eq '$($_.EmailAddress)'" if( $aduser ) { Write-Output "Adding user $($aduser.SamAccountName) to $GroupName" Add-ADGroupMember -Identity $GroupName -Members $aduser } else { Write-Warning "Could not find user in AD with email address $($_.EmailAddress)" } }
Confirm the Successful Addition of Users to the AD Security Group
After executing the PowerShell script to add users listed in a CSV file to an Active Directory (AD) Security Group, verifying the successful addition of these users is crucial. This verification process ensures that the intended users have been correctly added to the designated AD Security Group, confirming the script’s effectiveness and accuracy in managing group memberships.