We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script Repository

Export group members to CSV file

February 26, 2021 Views: 3105

The script exports a list of members of an AD group to a CSV file. The list includes both direct and indirect group members.


  • $csvFilePath - Specifies a full path to the CSV file that will be created by the script. In the path, the {0} placeholder will be replaced with the date and time of the export. You can use value references to specify a path. They will be replaced with corresponding property values of the group whose memberships are exported. For example, if you specify %name%, this text will be replaced with the group name.
Edit Remove
$csvFilePath = "\\server\share\%name%_MembershipReport-{0}.csv" # TODO: Modify me

$now = [System.DateTime]::Now.ToString("yyyy.MM.dd.HH.mm.ss")
$csvFilePath = [System.String]::Format($csvFilePath, $now)

# Get group members
    $memberGuidsBytes = $Context.TargetObject.GetEx("adm-MembersGuid")
    $Context.LogMessage("The group has no members. No CSV file will be created.", "Warning")
    return # Exit script

# Build report
$report = @()
foreach ($memberGuidBytes in $memberGuidsBytes)
    # Bind to the group member
    $memberGuid = New-Object "System.Guid" (, $memberGuidBytes)
    $memberGuid = $memberGuid.ToString("B")
    $memberPath = "Adaxes://<GUID=$memberGuid>"
    $member = $Context.BindToObject($memberPath)

    # Add member information to the report
    $memberName = [Softerra.Adaxes.Utils.ObjectNameHelper]::GetObjectName($member, 'IncludeParentPath')

    if ($Context.TargetObject.IsMember($memberPath))
        $membershipType = "Direct"
        $membershipType = "Indirect"
    $memberClass = $member.Class

    $reportEntry = New-Object PSObject
    $reportEntry | Add-Member -Name Name -Value $memberName -MemberType NoteProperty
    $reportEntry | Add-Member -Name Class -Value $memberClass -MemberType NoteProperty
    $reportEntry | Add-Member -Name "Membership Type" -Value $membershipType -MemberType NoteProperty
    $report += $reportEntry

# Export report to CSV
$report | Export-Csv -Path $csvFilePath -NoTypeInformation

Comments 0
Leave a comment

Got questions?

Support Questions & Answers