Tuesday, 7 January 2014

Powershell - Query Group Membership

A quick script to pull a list of users from a AD group:

## Query Group Membership ##
Import-Module ActiveDirectory
Get-ADGroup -Filter {Name -eq "ADGroup"} |
     ForEach-Object{
          $hash = @{GroupName = $_.Name ; UserID = '' ; Member = ''}
          $_ | Get-ADGroupMember -ea 0 -recurs |
              ForEach-Object{
                   $hash.Member = $_.Name
                   $hash.UserID = $_.SamAccountName
                   New-Object psObject -Property $hash
              }
          } | sort groupname, member | Export-CSV "C:\Data\ADGroup.csv" –NoTypeInformation

No comments:

Post a Comment