← script library
Manage Active Directory Group Memberships with PowerShell
Active DirectoryDecember 24, 2024
In this post, we will introduce a PowerShell script that helps IT administrators manage group memberships in Active Directory. Properly managing group memberships is essential for ensuring that users have the appropriate access to resources. This script allows you to quickly add or remove users from specified Active Directory groups, streamlining user management processes. Here is the PowerShell script for managing Active Directory group memberships:
powershell
# Import the Active Directory module
Import-Module ActiveDirectory
# Define the group and user details
$groupName = "YourADGroup"
$usersToAdd = @("user1", "user2") # Replace with actual usernames
$usersToRemove = @("user3") # Replace with actual usernames
# Add users to the group
foreach ($user in $usersToAdd) {
Add-ADGroupMember -Identity $groupName -Members $user
Write-Host "User $user has been added to group $groupName."
}
# Remove users from the group
foreach ($user in $usersToRemove) {
Remove-ADGroupMember -Identity $groupName -Members $user -Confirm:$false
Write-Host "User $user has been removed from group $groupName."
}Run it across your fleet
This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.
More in Active Directory
Active Directory User Onboarding with PowerShell
2026-08-17Active Directory User Offboarding with PowerShell
2026-08-17Disable an Active Directory User with PowerShell
2026-08-17Enable an Active Directory User with PowerShell
2026-08-17Unlock an Active Directory Account with PowerShell
2026-08-17Reset an Active Directory Password with PowerShell
2026-08-17Ready when you are.
Try ServerEngine free for 7 days.