1

I want to list the active directory name starting with prefix and its members.

Get-ADGroup gives me list of group names I wanted. for the Get-ADGroupMember command output I want to include group name, then list of members.

tried this code, but its giving me the error $_.Name. How do we include group name part of Get-ADGroupMember output?

Get-ADGroup -filter { name -like 'Test_*'  -and GroupCategory -eq 'Security' } |
 select-object -first 2  |ForEach-Object {
 Get-ADGroupMember -Identity $_.Name | select $_.name,Name
 }

Thanks

Ken White
  • 120,522
  • 13
  • 212
  • 426
sfgroups
  • 16,514
  • 23
  • 105
  • 182

1 Answers1

1

select $_.name,Name is a syntactical error, you need to use a calculated property to define the new property name and value:

$calculatedProps = @(
    @{
        Name = 'GroupName'
        Expression = { $groupName }
    }
    @{
        Name = 'Members'
        Expression = { $_.Name }
    }
)

Get-ADGroup -Filter "name -like 'Test_*' -and GroupCategory -eq 'Security'" |
    Select-Object -First 2 | ForEach-Object {
        $groupName = $_.Name

        Get-ADGroupMember -Identity $_.DistinguishedName |
            Select-Object $calculatedProps
    }

I would also recommend you to stop using script blocks on the -Filter parameter of the AD Module cmdlets to avoid future problems. Here is one example of the issues it might bring.

Santiago Squarzon
  • 20,988
  • 4
  • 10
  • 27