Last active
November 25, 2021 17:31
-
-
Save KianNH/c2812cdf9de817de8b490c04759b87ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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