AD group membership based on email address
Вставка
- Опубліковано 18 вер 2024
- To add users to an Active Directory group based on email addresses from a CSV file, you can follow these general steps using PowerShell. Make sure you have the necessary permissions to modify Active Directory groups.
Prepare the CSV File:
Create a CSV file with a column named "Email" containing the email addresses of the users you want to add to the Active Directory group. Save the file, for example, as "email.csv".
Create a PowerShell Script:
Create a PowerShell script (e.g., "AddUsersToADGroup.ps1") with the following content:
Load Active Directory module
Import-Module ActiveDirectory
Specify the path to the CSV file, In this example i am using my own csv path
$csvFilePath = "C:\ad_script\Email.csv"
Specify the name of the Active Directory group
$groupName = "Test_group"
Read and process each line in the CSV file
$users = Import-Csv $csvFilePath
foreach ($user in $users) {
$email = $user.Email
Get user object based on email
$userObj = Get-ADUser -Filter {EmailAddress -eq $email}
if ($userObj) {
Add user to the specified group
Add-ADGroupMember -Identity $groupName -Members $userObj
Write-Host "Added $($userObj.SamAccountName) to $groupName"
} else {
Write-Host "User with email $email not found."
}
}
Change the Group name as per your correct group name and also enter the correct the email id of users in csv file
#powershell
#powershellscripting
#powershelltraining
#activedirectory