×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Finding Parent Groups of users groups

Finding Parent Groups of users groups

Finding Parent Groups of users groups

(OP)
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!

CODE --> Powershell

$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

}

} 

RE: Finding Parent Groups of users groups

Herdrich,
Does this work for you? You'll need to fill in the value for $user

CODE --> Powershell

[bool]$new_groups_added = $TRUE
$user = ""  ### <<<<<<< Enter Username Here

[array]$groups = Get-ADPrincipalGroupMembership $user | foreach {$_.name}
$groups = $groups | sort

while ($new_groups_added)
	{
	 $new_groups_added = $FALSE
	 $holding_list = @()
	 foreach ($group in $groups)
		{
		 write-host "Checking: $group"
		 [array]$new_groups = Get-ADPrincipalGroupMembership $group | foreach {$_.name}
		 if ($new_groups.length -gt 1)
			{
			 $new_groups_added = $TRUE
			 foreach ($new in $new_groups)
				{$holding_list += $new}
			}
		 else
			{$holding_list += $group}
		}
	 [array]$groups = $holding_list
	 $groups = $groups | sort
	 $groups = $groups | select -Unique
	 if ($new_groups_added)
		{
		 write-host ""
		 write-host "--------------------------"
		 write-host "     Starting Recheck"
		 write-host "--------------------------"
		 write-host ""
		}
	}

## $groups  ### <<<<<<< List of groups is stored here 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Finding Parent Groups of users groups

(OP)
Nice! Looks to be working. Just have to fix up my inputs and outputs. Thanks :)

RE: Finding Parent Groups of users groups

Now I think that only gets the parent groups. It does not include the child. If you want the children included with the parents, you need to make a small modification to the script: Add the lines in GREEN and remove the lines in RED

CODE --> Powershell

[bool]$new_groups_added = $TRUE
$user = ""  ### <<<<<<< Enter Username Here

$master_list = @()

[array]$groups = Get-ADPrincipalGroupMembership $user | foreach {$_.name}
$groups = $groups | sort

while ($new_groups_added)
	{
	 $new_groups_added = $FALSE
	 $holding_list = @()
	 foreach ($group in $groups)
		{
		 $master_list += $group
		 write-host "Checking: $group"
		 [array]$new_groups = Get-ADPrincipalGroupMembership $group | foreach {$_.name}
		 if ($new_groups.length -gt 1)
			{
			 $new_groups_added = $TRUE
			 foreach ($new in $new_groups)
				{$holding_list += $new}
			}
                 else
			{$holding_list += $group}
		}
	 [array]$groups = $holding_list
	 $groups = $groups | sort
	 $groups = $groups | select -Unique
	 if ($new_groups_added)
		{
		 write-host ""
		 write-host "--------------------------"
		 write-host "     Starting Recheck"
		 write-host "--------------------------"
		 write-host ""
		}
	}
$master_list = $master_list | sort
$master_list = $master_list | select -Unique
## $master_list  ### <<<<<<< List of groups is stored here 


Light travels faster than sound. That's why some people appear bright until you hear them speak.

RE: Finding Parent Groups of users groups

(OP)
I was just looking for the parent groups so it worked perfectly :)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close