Get upn and samaccount name for users mentioned on directreports

Iron Contributor

Hello all

I am trying to get all disabled user accounts that have users listed on the disabled accounts "directreports" . The below code is showing me the disabled account and the directreports distinguishedname, however i am getting the following error

 

error1.GIF

Get-ADUser -Filter * -Properties samaccountname,directreports,enabled | where {$_.directreports -notlike $null -and $_.enabled -eq $False}
             ForEach-Object{
             $mgr = $_
             $_.directreports |
                 ForEach-Object{
                     [array]$d += Get-ADObject $_ |
                                     Select-Object UserPrincipalName, samaccountname
                 }
             $d |
                 ForEach-Object{
                     [PSCustomObject]@{
                         samaccountname = $mgr.samaccountname
                         enabled = $mgr.Enabled
                         distinguishedname = $mgr.distinguishedname
                         directreportupn = $_.userprincipalname
                         directteportsamaccountname = $_.samaccountname
                     }
                 }
             } 

 

 

2 Replies

 

$users = Get-ADUser -Filter * -Properties samaccountname, directreports, enabled | Where-Object { $_.directreports -notlike $null -and $_.enabled -eq $False }
ForEach ($user in $users) {
    $directreportsupn = (get-aduser -filter * | Where-Object DistinguishedName -Match $user.directreports).UserPrincipalName
    $directreportssamaccountname = (get-aduser -filter * | Where-Object DistinguishedName -Match $user.directreports).SamAccountName
    [PSCustomObject]@{
        samaccountname             = $user.samaccountname
        enabled                    = $user.Enabled
        distinguishedname          = $user.distinguishedname
        directreportupn            = $directreportsupn
        directteportsamaccountname = $directreportssamaccountname
    }
}

 

@Skipster311-1 Changed the code a little bit, could you verify and let me know if this is what you want?

Did this work for you?