Powershell get Users Manager

In Below code I have filtered user by employee Id you can filter by name or SAMAccount

$ADRoot = [ADSI]”

$EE_NO = ‘12700’
$ADSearch = New-Object System.DirectoryServices.DirectorySearcher($ADRoot)
$SAMAccountName = “$Username”
$employeeid = “$Username”

  1. $ADSearch.Filter = “(&(objectClass=user)(sAMAccountName=$SAMAccountName))”

$ADSearch.Filter = “(&(objectClass=user)(employeeid=$EE_NO))”
$Result = $ADSearch.FindAll()

If($Result.Count -eq 0)
{
Write-Host “No such user on the Server” $Username | Out-Null

}
Else
{
foreach($user in $Result)
{
$val = $user.path
$account=[ADSI]$val
$mgr = $account.PsBase.InvokeGet(“manager”)

  1. $acc = “sdcorp\” + $SAMAccountName.Trim()

$acc = $mgr;
}
}

Advertisements