skip to Main Content

Remove users from group with PowerShell

How to bulk remove users from AD security group with PowerShell? You can select multiple users in AD and remove them from the group, but what if you have a list of users and you don’t want to find them one by one? This is an excellent way to automate the task with PowerShell. In this article, you will learn how to bulk remove users from a group with PowerShell script.

Information

You need to remove a list of users in CSV file from a security group. The list is populated with the UserPrincipalName attribute.

There are two options to accomplish the task:

  • Manually search for the users in Active Directory Users and Computers, and remove them from the security group. If you have a long list, this is time-consuming. Not only that, it’s possible that you miss a user from the list.
  • Automate the search with PowerShell, and remove the users from the security group. It will take less time, and you will not miss any users.

PowerShell is great for automation, and that’s what we recommend using.

Check security group

In this example, we have the security group Pilot. The members section is filled with the users.

Remove users from group with PowerShell before

Read more: List all users in a Security Group through PowerShell »

Check CSV file with Import-Csv cmdlet

Check the CSV file and that you use the correct header. In our case, it’s the CSV file Users.csv and the header UserPrincipalName.

Remove users from group with PowerShell CSV file UPN
Check that there are no empty spaces behind each line. If so, you will get errors, and the script will fail to remove the users.

An excellent way is to add quotation marks to surround the field.

Remove users from group with PowerShell CSV file UPN quotation marks

Place the CSV file in C:\Temp folder. Create a temp folder if you don’t have one.

Remove users from group with PowerShell CSV file

Run Windows PowerShell as administrator. Make sure that PowerShell can read the file, run Import-Csv cmdlet.

PS C:\> Import-Csv C:\Temp\Users.csv

UserPrincipalName      
-----------------
Amanda.Morgan@exoip.com
Max.Fraser@exoip.com
Kylie.Davidson@exoip.com
Richard.Grant@exoip.com
Boris.Campbell@exoip.com
Nicholas.Murray@exoip.com
Leonard.Clark@exoip.com
Ruth.Dickens@exoip.com
John.Maverick@exoip.com
Ali.Tajran@exoip.com
Alysia.Maverick@exoip.com
Mohammad.Fistak@exoip.com

Remove users from group PowerShell script

Download Remove-ADUsers.ps1 PowerShell script or copy and paste the below code in Notepad. Give it the name Remove-ADUsers.ps1 and place it in the C:\scripts folder. Create a scripts folder if you don’t have one.

# Import AD Module
Import-Module ActiveDirectory

# Import the data from CSV file and assign it to variable
$Users = Import-Csv "C:\Temp\Users.csv"

# Specify target group where the users will be removed from
# You can add the distinguishedName of the group. For example: CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local
$Group = "Pilot" 

# Retrieve UPN
foreach ($User in $Users) {
    $UPN = $User.UserPrincipalName
    Get-ADUser -Filter "UserPrincipalName -eq '$UPN'" |

    # Remove user from group
    ForEach-Object { Remove-ADGroupMember -Identity "$Group" -Members "$_" -Confirm:$false } 
}
  • Line 5: Edit the CSV file name and the path.
  • Line 9: Edit the target OU.

In the next step, we will have a look at the bulk remove AD Users PowerShell script.

Bulk remove users from group with CSV file

Change the path to the scripts folder. Run the PowerShell script to bulk remove AD users from group. Wait till it completes.

PS C:\> cd c:\scripts
PS C:\scripts> .\Remove-ADUsers.ps1

Verify security group with removed users

When the script finishes, have a look at Active Directory Users and Computers. Go to the security group and validate that you don’t see the users from the CSV file in the members tab. In this case, the Pilot group.

Remove users from group with PowerShell after

Everything looks great! Did this help you to bulk remove users from security group with PowerShell?

Keep reading: Active Directory country code list »

Conclusion

In this article, you learned how to remove users from group with PowerShell. Download the Remove-ADUsers.ps1 PowerShell script, edit the CSV path and the target group. Run the script and verify that the AD users are removed successfully from the group. PowerShell is great for automating the process.

Did you enjoy this article? You may also like PowerShell remove quotation marks from a text file. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 0 Comments

Leave a Reply

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

Back To Top