I have a list with AD groups. I want to check if each of these groups is a MemberOf a parent group(s). If so, I want to list the parent group(s). As an example: Child group 1 has no parent group, so it does not have to list anything. Child group 2 has 2 parent groups (so is member of 2 groups), I want to list those 2 groups. And so on...
I started with a simple code:
get-adgroup -filter "CN -eq 'Child2'" -properties memberof | select memberof
The result I get is almost what I want:
memberof
{CN=Parent1,OU=ABC, CN=Parent2,OU=ABC}
So this works, allthough I prefer not to see the 'CN=' part and 'OU=' part, just the groupname(s).
Next step I tried below code:
Import-Csv -Path H:\Test\Input_ADGroup.csv |
ForEach-Object {
$Group = Get-ADGroup -filter "CN -eq '$($_.CN)'" -properties memberof
[PSCustomObject]@{
SourceCN = $_.CN
MemberOf = $Group.memberof
}
} | Export-Csv -Path H:\Test\Output_ADGroup.csv -NoTypeInformation
When using the code above, it does not work correctly. It shows a list with the input groups (child groups) but the output groups (parent groups) is shown as: "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection" Somehow it does not work when the output contains 2 or more (parent) groups.
Another option I tried was using the Get-ADPrincipalGroupMembership
function, but this always give me an error: Get-ADPrincipalGroupMembership : The operation being requested was not performed because the user has not been authenticated
Anyone has some ideas how to help me getting the parent groups of each AD groups I have in a file?
Thanks in advance.