Manage Active Directory Group Memberships with PowerShell

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:

# 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."
}