Josh
Josh

Reputation: 111

Getting AD groups and their users

I've been trying to get a list of all the groups in our AD environment (with the description) and their members and output it to a CSV file. Ideally the users would be shown under their group. The script I've been trying to use is:

Import-Module ActiveDirectory

Get-ADGroup -Filter * -Properties Description |
    Select-Object Name, Description |
    ForEach-Object {
        Get-ADGroupMember -Identity $_.DistinguishedName -Recursive |
            Get-ADObject -Properties SamAccountname, Title, Department |
            Select-Object Name, SamAccountName, Title, Department, DistinguishedName, ObjectClass
    } | Export-Csv -Path c:\temp\ADGrab.csv -NoTypeInformation

The error I keep getting is as follows:

Get-ADGroupMember : Cannot validate argument on parameter 'Identity'. The argument
is null or empty. Supply an argument that is not null or empty and then try the
command again.
At C:\Users\j_kennedy_ta\AppData\Local\Temp\9\2898ceb2-a6cf-4fbf-9341-e651dad2145d.ps1:4 char:28
+ Get-ADGroupMember -Identity <<<<  $_.distinguishedname -Recursive |
    + CategoryInfo          : InvalidData: (:) [Get-ADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADGroupMember

Upvotes: 1

Views: 357

Answers (1)

sodawillow
sodawillow

Reputation: 13176

Without the nasty Select-Object and with group information in the CSV file:

Import-Module ActiveDirectory

Get-ADGroup -Filter * -Properties Description |
    ForEach-Object {

        # Store for later use
        $groupName = $_.Name
        $groupDescription = $_.Description

        Get-ADGroupMember -Identity $_.DistinguishedName -Recursive |
            Get-ADObject -Properties SamAccountname, Title, Department |
            Select-Object Name, SamAccountName, Title, Department, DistinguishedName, ObjectClass, ` # Mind the gap
            # Calculated properties with group information
            @{ name = "GroupName"; expression = $groupName }, `
            @{ name = "GroupDescription"; expression = $groupDescription }
    } | Export-Csv -Path c:\temp\ADGrab.csv -NoTypeInformation

Upvotes: 1

Related Questions