How to add/remove Active Directory users to Active Directory groups using PowerShell

How to add/remove Active Directory users to Active Directory groups using PowerShell


Active Directory is a powerful tool for managing users and groups in a Windows environment. One of the most often-repeated tasks for administrators is to add or remove users from Active Directory groups. In this article, we will explore how to perform this task using PowerShell.


Adding Users to Active Directory Groups


To add a user to an Active Directory group, you can use the following PowerShell cmd:


  1. Add-ADGroupMember -Identity "GroupName" -Members "User1", "User2"


Where “GroupName” = the name of the group in which you want the users to be added. 

Multiple users can be specified in the above command, by separating each of the user names with commas.


For example, to add the users "SalesGuy1" and "SalesDude2" to the group "Sales", use the following command:


  1. Add-ADGroupMember -Identity "Sales" -Members "SalesGuy1", "SalesDude2"


If you need to add a large number of users to a group, you can import a CSV file containing the users. For example, you can create a file named "SalesUsers.csv" that contains a list of users in the following format:


Username
SalesGuy1
SalesDude2


Then, you can use the following command to add the users in the CSV file to the "Sales" group:


  1. Import-Csv "C:\SalesUsers.csv" | ForEach-Object {Add-ADGroupMember -Identity "Sales" -Members $_.Username}


This command uses the Import-Csv cmdlet to read the CSV file and then channels the results to the ForEach-Object cmdlet. The ForEach-Object cmdlet then calls the Add-ADGroupMember cmdlet for each user in the file.



Removing Users from Active Directory Groups


To remove a user from an Active Directory group, you can use the following PowerShell command:


  1. Remove-ADGroupMember -Identity "GroupName" -Members "User1", "User2"


Where “GroupName” = the name of the group in which you want the users to be added.

Like above, multiple users can be specified in the above command, by separating each of the user names with commas.


For example, to remove the users "SalesGuy1" and "SalesDude2" from the group "Sales", use the following command:


  1. Remove-ADGroupMember -Identity "Sales" -Members "SalesGuy1", "SalesDude2"


Similar to adding users, you can also use a CSV file to specify the users to remove from a group. For example, you can create a file named "SalesUsers.csv" that contains a list of users in the following format:


Username
SalesGuy1
SalesDude2


Then, you can use the following command to remove the users in the CSV file from the "Sales" group:


  1. Import-Csv"C:\SalesUsers.csv" | ForEach-Object {Remove-ADGroupMember -Identity"Sales" -Members $_.Username}


This command uses the same approach as the one used for adding users. It reads the CSV file and pipes the results to the ForEach-Object cmdlet, which calls the Remove-ADGroupMember cmdlet for each user in the file.



    • Related Articles

    • Generate an Activity Report for Microsoft 365 Groups and Teams

      Introduction The activity reports available for Microsoft 365 groups and Teams can be beneficial for administrators in an organization. Microsoft 365 teams group activity reports provide insight into group activities, group workloads, group counts, ...
    • How to find unused Exchange Online mailboxes

      What are unused Exchange Online mailboxes and how to identify them? Unused Exchange Online mailboxes are user mailboxes which are currently not being used by their users. There are 3 ways in which we can identify if a mailbox is unused or not. They ...
    • How to create, copy and remove Active Directory users using PowerShell

      One of the most fundamental and often repeated task by an administrator is to create, copy or remove an Active directory user. Thanks to the New-ADUser cmdlet, it is extremely simple. Below are the PowerShell scripts to create, copy and remove an ...
    • How to create and manage Microsoft 365 groups with PowerShell

      Introduction: Microsoft 365 Groups is a collaboration feature that allows users to work together and share resources such as calendars, files, and email messages. Microsoft 365 Groups can be created and managed using the Microsoft 365 admin center, ...
    • Finding all groups that contain the same members in Active Directory

      Active Directory users are put into groups for categorization and access privilege purposes. However, as an organization grows, there may be instances of repetition of groups, or an administrator may be required to monitor the groups that a specific ...