Skip to content

Instantly share code, notes, and snippets.

@nickadam
Created September 17, 2019 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickadam/a7814dfebbadec7eb526b9191a7d7bb9 to your computer and use it in GitHub Desktop.
Save nickadam/a7814dfebbadec7eb526b9191a7d7bb9 to your computer and use it in GitHub Desktop.
Get-Subordinates.ps1
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