← 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.

Ready when you are.

Try ServerEngine free for 7 days.