February 1, 2012

Active Directory - Groups and users

Export information about AD groups and its members to csv file:

$secGroups = get-adgroup -filter * |
  select-object -expandproperty DistinguishedName |
  sort-object
& {
    foreach ($secGroup in $secGroups) {
      $members = get-adgroupmember -identity $secGroup |
        select-object -expandproperty DistinguishedName |
        sort-object
      foreach ($member in $members) {
          "" | select-object @{Name="Group"; Expr={$secGroup}},
            @{Name="Member"; Expr={$member}}
      }
    }
} | export-csv GroupsAndMembers.csv -notypeinformation

No comments:

Post a Comment