Created
September 17, 2019 18:37
-
-
Save nickadam/a7814dfebbadec7eb526b9191a7d7bb9 to your computer and use it in GitHub Desktop.
Get-Subordinates.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-Subordinates(){ | |
param( | |
[Parameter(Mandatory=$True, | |
HelpMessage='The username of the person at the top of the org')] | |
[string]$Username, | |
[Parameter(Mandatory=$False, | |
HelpMessage='The level to traverse into the org, i.e. 1 = only include direct reports, -1 = all')] | |
[int]$Depth | |
) | |
$User = Get-ADUser $Username | |
if(!$User){ | |
Throw "User not found" | |
} | |
if(($Depth -ne 0) -and ($Depth -ne -1)){ | |
$Depth = $Depth - 1 | |
} | |
Get-ADUser -Filter {Manager -eq $User.DistinguishedName} -Properties Mail,Manager,DisplayName | ForEach{ | |
$User = $_ | |
$User | |
if($Depth -ne 0){ | |
Get-Subordinates -Username $User.SamAccountName -Depth $Depth | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment