Hello All,
I am trying to loop this until it hits the top level parent groups. Currently I get the users group then the group of those groups. I want to be able to go up until I hit the top group. I could probably nest a few more foreach statements but there has to be a cleaner way to do this. any help would be great!
I am trying to loop this until it hits the top level parent groups. Currently I get the users group then the group of those groups. I want to be able to go up until I hit the top group. I could probably nest a few more foreach statements but there has to be a cleaner way to do this. any help would be great!
Code:
$Groups = GET-ADUser -Identity USERNAME –Properties MemberOf | Select-Object -ExpandProperty MemberOf | Get-ADGroup -Properties name | where { $_.GroupCategory -eq "Security" } | sort | Select-Object $_.name
foreach ($Group in $Groups)
{
Get-ADGroup -Identity $Group –Properties MemberOf | Select-Object -ExpandProperty MemberOf | Get-ADGroup -Properties name | where { $_.GroupCategory -eq "Security" } | sort | Select-Object name
$Groups2=Get-ADGroup -Identity $Group –Properties MemberOf | Select-Object -ExpandProperty MemberOf | Get-ADGroup -Properties name | where { $_.GroupCategory -eq "Security" } | sort | Select-Object $_.name
foreach ($Group in $Groups2)
{
Get-ADGroup -Identity $Group –Properties MemberOf | Select-Object -ExpandProperty MemberOf | Get-ADGroup -Properties name | where { $_.GroupCategory -eq "Security" } | sort | Select-Object name
}
}