List Active Directory Group Members with PowerShell

Managing group memberships in Active Directory is crucial for maintaining security and ensuring proper access control. In this post, we will share a PowerShell script that helps you list all the members of a specified Active Directory group. This script can be especially helpful for administrators who need to review group memberships regularly.
At ServerEngine, we strive to provide valuable tools for optimizing server management. Visit [ServerEngine](https://serverengine.co) to explore our innovative solutions.
### Step 1: Prepare Your Environment
Make sure you have the Active Directory module installed and imported into your PowerShell session to use the necessary cmdlets.
“`powershell

Import-Module ActiveDirectory

### Step 2: Define the Function to List Group Members
We will create a function named `Get-ADGroupMembers` that takes the name of an AD group as a parameter and retrieves its members.
“`powershell

function Get-ADGroupMembers {
    param (
        [string]$groupName
    )
    try {
        $members = Get-ADGroupMember -Identity $groupName
        $members | Select-Object Name, SamAccountName, ObjectClass | Format-Table -AutoSize
    } catch {
        Write-Host "ERROR: Could not retrieve members for group: $groupName. Error: $_"
    }
}

### Step 3: Execute the Function
To use the function, specify the name of the group whose members you want to list.
“`powershell

Get-ADGroupMembers -groupName "YourGroupName"

### Conclusion
This PowerShell script offers a quick and efficient way to view members of any Active Directory group, making it easier to manage group memberships effectively. For more advanced server management tools, make sure to check out [ServerEngine](https://serverengine.co).