2. Active Directory Tasks - Powershell
2.8 Adding Users and Computers to a Group
Add Users to a Group
we could use the Add-ADGroupMember
cmdlet to add users or computers to a group.
to add a user named “Ammar Saber” with SamAccountName of (ammar.saber) to a group named “IT-Support”:
Add-ADGroupMember -Identity "IT-Support" -Members "ammar.saber"
Note: you could add multiple users or computers to a group by separating them with a comma.
to list all the members of that group:
Get-ADGroupMember -Identity "IT-Support"
To add a user to multiple groups at once, run the following script.
"IT-Support","Quality" | Add-ADGroupMember -Members (Read-Host -Prompt "Enter User Name")
Add Users from a CSV File
you can also add users from a CSV file using the following script:
Import-Csv C:\Users\domain-admin\Desktop\users.csv | ForEach-Object { Add-AdGroupMember -Identity "IT-Support" -Members $_.users -Verbose }
file content:
If you want to copy all members from “IT-Support” group to “IT-Admins” group, run the following script:
Get-ADGroupMember "IT-Support" | Get-ADUser | ForEach-Object {Add-ADGroupMember -Identity "IT-Admins" -Members $_}
Add Computers to a Group
you will use the same cmdlet to add computers to a group.
Add-ADGroupMember -Identity "IT-Support" -Members WS-IT-01$
Don’t forget to add the
$
at the end of the computer name.