Steps to add User to Active Directory Group Using Add-ADGroupMember

The Add-ADGroupMember cmdlet in the Active Directory Module for Windows PowerShell can be used to add users to Active Directory distribution or security groups. To utilise cmdlets from the ActiveDirectory module, you must first load it into your PowerShell session (this module is automatically installed on domain controllers with Windows Server 2012 or higher):

Import-Module ActiveDirectory

To add user1 to the domain group “TestGroup1”, run the following command in the PowerShell console with administrator privileges:

Add-ADGroupMember "TestGroup1" user1

You can add several users to the group at once, their accounts should be enumerated by comma:

Add-ADGroupMember "NYTraders" KDunkelman,SSmith

These are the most basic instances of adding users to AD groups with the Add-ADGroupMember cmdlet. Let’s have a look at some more advanced ways. 

For instance, you may need to obtain a list of users from one AD group (NYTraders) and add these accounts to another AD group (USTraders). We’ll use the Get-ADGroupMember cmdlet to get a list of users in the NYTraders group. The command that results might look like this: 

Get-ADGroupMember “NYTraders” | Get-ADUser | ForEach-Object {Add-ADGroupMember -Identity “USTraders” -Members $_}

You can add all users from a specific OU to the group:


Get-ADUser -Filter * -SearchBase ‘OU=Users,OU=NY,OU=USA,DC=solutionviews,DC=com’|

ForEach-Object -process {Add-ADGroupMember -identity "NY Users" -Members $_.SamAccountName}

You can open the ADUC console after running the command to ensure that all users have been added to the selected group.

You can choose users based on the value of an AD property and then add them to a group. For example, run the command to add all users to the USAUsers group who have the United States in their co field:

Get-ADUser -filter {(co -eq "United States")} | ForEach-Object -process {Add-ADGroupMember -identity "USAUsers" -Members $_.SamAccountName}

You can make a list of users you want to add to a specific AD group in an Excel file (or a text CSV file). The file should contain a list of your users’ samAccountNames. You can save your files in the following format:

The following is the code for a PowerShell script that adds users from a CSV file to a group:

$List = Import-CSV .users.csv

$ErrorActionPreference='Continue'

$error.Clear()

ForEach ($User in $List)

{

Add-ADGroupMember -Identity ‘USTraders’ -Member $User.username

}

if ($error.Count -gt 0)

{

echo "Errors count: " $error.Count

}

$success=$($i-$error.Count)

if ($success -gt -1)

{

echo $success " users added successfully"

}

Similarly, you can add users to the Exchange distribution group:

Import-CSV .Users.csv | ForEach-Object -process {Add-DistributionGroupMember -Identity "USTradersMailList" -Member $_.username }

Leave a Reply

Your email address will not be published. Required fields are marked *




Enter Captcha Here :