Forum Discussion
Diego13
Nov 09, 2022Copper Contributor
Get nested AD groups from bulk user or OU
Hi, I would like to export in a csv file all groups and nested groups from bulk AD user or OU. Example: Users Groups NestedGroups User1 Group1 NestedGroup1 ...
Alan2022
Nov 10, 2022Iron Contributor
# Filter Groups in AD based on OU
$searchBase = "OU=Test,DC=company,DC=com"
Get-ADGroup -Filter * -SearchBase $searchBase -Property * | Sort-Object Name
# Get List of Members base on Group Name
Get-ADGroupMember -Identity '<GroupName>' -Recursive | Select-Object -Property @{n="Members"; e={ $_.Name }} | Sort-Object Members
Diego13
Nov 10, 2022Copper Contributor
Hi Alan, thanks for the powershell provided.
Is it possible to export the results as requested please ?
Is it possible to export the results as requested please ?
- Alan2022Nov 11, 2022Iron Contributor
Diego13
Hi,Yes you can just save them in a variable like $result. Then use export to csv ps script.
$result | Export-Csv c:\temp\test.csv -NoTypeInformation- Diego13Nov 11, 2022Copper ContributorHi,
I have this error now when adding "$result | Export-Csv c:\temp\test.csv -NoTypeInformation" at the end of the script:
Get-ADGroupMember : Cannot find an object with identity: 'GroupName' under: 'DC=...'- Diego13Nov 11, 2022Copper ContributorComplete error:
Get-ADGroupMember : Cannot find an object with identity: 'GroupName' under: 'DC=...,DC=...'.
At line:6 char:1
+ Get-ADGroupMember -Identity 'GroupName' -Recursive | Select-Object -P ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (GroupName:ADGroup) [Get-ADGroupMember], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADGroupMember
Get-ADGroup : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At line:7 char:23
+ Get-ADGroup -identity $item.GroupName | select name | Export-csv -pat ...
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADGroup], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADGroup