Get-ADGroupMembers.ps1


Description

Purpose

Get-ADGroupMembers

Detailed Description

This function exports the users within Active Directory groups that match a specified search string.

Back to Top

Usage

Example 1

Get-ADGroupMembers -GroupName "Domain Admins"

Outputs a list of users in the Active Directory groups matching the search string.

Back to Top

Notes

Author: Luke Leigh Date: 05/07/2023 Version: 0001 Changelog:

  • initial version

Back to Top


Script

<#
.SYNOPSIS
    Get-ADGroupMembers

.DESCRIPTION
    This function exports the users within Active Directory groups that match a specified search string.

.PARAMETER GroupName
    Specifies the name of the group to search for. This parameter supports wildcards.

.EXAMPLE
    Get-ADGroupMembers -GroupName "Domain Admins"
    Outputs a list of users in the Active Directory groups matching the search string.

.NOTES
    Author: Luke Leigh
    Date: 05/07/2023
    Version: 0001
    Changelog:
        - initial version

.INPUTS
    [string]GroupName
#>
function Get-ADGroupMembers {
    [CmdletBinding(DefaultParameterSetName = 'Default',
        PositionalBinding = $true,
        SupportsShouldProcess = $true)]
    param
    (
        [Parameter(ParameterSetName = 'Default',
            Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            Position = 1,
            HelpMessage = 'Enter the group name that you want to search for. This field supports wildcards.')]
        [String]$GroupName = '*'
    )

    begin {
        # Update-FormatData -PrependPath "$PSScriptRoot\Get-ADGroupMembers.Format.ps1xml"
    }

    process {
        if ($PSCmdlet.ShouldProcess("$GroupName", "Extract members of group")) {
            $groups = Get-ADGroup -Filter "Name -like '$GroupName'"
            foreach ($group in $groups) {
                $groupMembers = Get-ADGroupMember -Identity $group.SamAccountName
                foreach ($member in $groupMembers) {
                    if ($member.objectClass -eq "user") {
                        $user = Get-ADUser -Identity $member.SamAccountName -Properties *
                        Write-Output $user
                    }
                }
            }
        }
    }
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top