Skip to content

Instantly share code, notes, and snippets.

@KianNH
Last active November 25, 2021 17:31
Show Gist options
  • Save KianNH/c2812cdf9de817de8b490c04759b87ba to your computer and use it in GitHub Desktop.
Save KianNH/c2812cdf9de817de8b490c04759b87ba to your computer and use it in GitHub Desktop.
# get all of the groups under a provided organizational unit (recursive)
$groups = Get-ADGroup -Filter * -SearchBase '<distinguishedName>' -Properties Description
# loop through all the groups we just got
foreach ($group in $groups) {
# get the members of the group, and loop through those members
Get-ADGroupMember -Identity $group | foreach {
# create a pscustomobject to store our data
[pscustomobject]@{
groupname = $group.Name
groupdescription = $group.Description
samaccountname = $_.samAccountName
displayname = $_.displayName
lastlogondate = $_.lastLogonDate
enabled = $_.enabled
} | # pipeline this new object to our csv and append it
Export-Csv -Path "C:\Temp\Security Groups.csv" -Append -NoTypeInformation
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment