param ( [alias('U')] [string]$UserName = '', [alias('G')] [string]$GroupName = '', [alias('S')] [string]$Server = '' ) function Get-ADGroupTreeViewMemberOf { #requires -version 4 <# .SYNOPSIS Show UpStream tree view hierarchy of memberof groups recursively of a Active Directory user and Group. .DESCRIPTION The Show-ADGroupTreeViewMemberOf list all nested group list of a AD user. It requires only valid parameter AD username, .PARAMETER UserName Prompts you valid active directory User name. You can use first character as an alias, If information is not provided it provides 'Administrator' user information. .PARAMETER GroupName Prompts you valid active directory Group name. You can use first character as an alias, If information is not provided it provides 'Domain Admins' group[ information. .INPUTS Microsoft.ActiveDirectory.Management.ADUser .OUTPUTS Microsoft.ActiveDirectory.Management.ADGroup .NOTES Version: 1.0 Author: Kunal Udapi Creation Date: 10 September 2017 Purpose/Change: Get the exact nested group info of user Useful URLs: http://vcloud-lab.com .EXAMPLE PS C:\>.\Get-ADGroupTreeViewMemberOf -UserName Administrator This list all the upstream memberof group of an user. .EXAMPLE PS C:\>.\Get-ADGroupTreeViewMemberOf -GroupName DomainAdmins This list all the upstream memberof group of a Group. #> [CmdletBinding(SupportsShouldProcess=$True, ConfirmImpact='Medium', HelpURI='http://vcloud-lab.com', DefaultParameterSetName='User')] Param ( [parameter(ParameterSetName = 'User',Position=0, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, HelpMessage='Type valid AD username')] [alias('User')] [String]$UserName = 'Administrator', [parameter(ParameterSetName = 'Group',Position=0, ValueFromPipelineByPropertyName=$true, ValueFromPipeline=$true, HelpMessage='Type valid AD Group')] [alias('Group')] [String]$GroupName = 'Domain Admins', [parameter(ParameterSetName = 'Group', DontShow=$True)] [parameter(ParameterSetName = 'User', DontShow=$True)] [alias('S')] [String]$Server = 'localhost', [alias('U')] $UpperValue = [System.Int32]::MaxValue, [parameter(ParameterSetName = 'Group', DontShow=$True)] [parameter(ParameterSetName = 'User', DontShow=$True)] [alias('L')] $LowerValue = 2 ) begin { if (!(Get-Module Activedirectory)) { try { Import-Module ActiveDirectory -ErrorAction Stop } catch { Write-Host -Object "ActiveDirectory Module didn't find, Please install it and try again" -BackgroundColor DarkRed Break } } switch ($PsCmdlet.ParameterSetName) { 'Group' { try { $Group = Get-ADGroup -Server $Server $GroupName -Properties Memberof -ErrorAction Stop $MemberOf = $Group | Select-Object -ExpandProperty Memberof $rootname = $Group.Name } catch { Write-Host -Object "`'$GroupName`' groupname doesn't exist in Active Directory, Please try again." -BackgroundColor DarkRed $result = 'Break' Break } break } 'User' { try { $User = Get-ADUser -Server $Server $UserName -Properties Memberof -ErrorAction Stop $MemberOf = $User | Select-Object -ExpandProperty Memberof -ErrorAction Stop $rootname = $User.Name } catch { Write-Host -Object "`'$UserName`' username doesn't exist in Active Directory, Please try again." -BackgroundColor DarkRed $result = 'Break' Break } Break } } } Process { $Minus = $LowerValue - 2 $Spaces = " " * $Minus $Lines = "__" "{0}{1}{2}{3}" -f $Spaces, '|', $Lines, $rootname $LowerValue++ $LowerValue++ if ($LowerValue -le $UpperValue) { foreach ($member in $MemberOf) { $UpperGroup = Get-ADGroup -Server $Server $member -Properties Memberof $LowerGroup = $UpperGroup | Get-ADGroupMember -erroraction 'silentlycontinue' $LoopCheck = $UpperGroup.MemberOf | ForEach-Object {$lowerGroup.distinguishedName -contains $_} if ($LoopCheck -Contains $True) { $rootname = $UpperGroup.Name Write-Host "Loop found on $($UpperGroup.Name), Skipping..." -BackgroundColor DarkRed Continue } #"xxx $($LowerGroup.name)" #$Member #"--- $($UpperGroup.Name) `n" Get-ADGroupTreeViewMemberOf -Server $Server -GroupName $member -LowerValue $LowerValue -UpperValue $UpperValue } #foreach ($member in $MemberOf) { } } #Process } # Show Message and return bool. # $asd = Read-MessageBoxDialog -Message "Please press the OK button." -WindowTitle "Message Box Example" -Buttons OKCancel -Icon Exclamation function Read-MessageBoxDialog( [string]$Message, [string]$WindowTitle, [System.Windows.Forms.MessageBoxButtons]$Buttons = [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]$Icon = [System.Windows.Forms.MessageBoxIcon]::None ){ Add-Type -AssemblyName System.Windows.Forms return [System.Windows.Forms.MessageBox]::Show($Message, $WindowTitle, $Buttons, $Icon) } # Show input box popup and return the value entered by the user. # $textEntered = Read-InputBoxDialog -Message "Please enter the word 'Banana'" -WindowTitle "Input Box Example" -DefaultText "Apple" function Read-InputBoxDialog([string]$Message, [string]$WindowTitle, [string]$DefaultText) { Add-Type -AssemblyName Microsoft.VisualBasic return [Microsoft.VisualBasic.Interaction]::InputBox($Message, $WindowTitle, $DefaultText) } function ShowText([string]$Message, [string]$WindowTitle){ Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $window = New-Object System.Windows.Forms.Form # $window.Width = 1000 # $window.Height = 800 $Label = New-Object System.Windows.Forms.Label $Label.Location = New-Object System.Drawing.Size(10,10) $Label.Text = $Message $Label.AutoSize = $True $window.Controls.Add($Label) [void]$window.ShowDialog() } if ($Server -eq $null -or $Server -eq ""){ $Server = Read-InputBoxDialog -Message "Please enter an AD Server" -WindowTitle "Enter Server" -DefaultText "localhost" } if ($Server -eq $null -or $Server -eq ""){ return } # if ($args[0] -eq $null -or $args[0] -eq ""){ if ($UserName -eq "" -and $GroupName -eq ""){ $UserName = Read-InputBoxDialog -Message "Please enter an AD Username or cancel to enter Group Name" -WindowTitle "Enter Username" -DefaultText "Administrator" } if ($UserName -eq ""){ if ($GroupName -eq ""){ $GroupName = Read-InputBoxDialog -Message "Please enter an AD Group Name" -WindowTitle "Enter Group" -DefaultText "Domain Users" } if ($GroupName -eq ""){ return } Get-ADGroupTreeViewMemberOf -GroupName $GroupName -Server $Server } Get-ADGroupTreeViewMemberOf -UserName $UserName -Server $Server pause