Get-ADUser properties without the Active Directory Module

I stumbled over this topic while creating a script on client PCs. The script was relying on information stored in the active directory and was running in the logged in users context. Due to limitations on the client machine, I was not able to install the windows feature “Active Directory Module for Windows PowerShell” on all the clients. That’s why I came up with the following function.

function Get-ADUserInfos {
    Param
    (
        [Parameter(Mandatory = $true,
            ValueFromPipelineByPropertyName = $true,
            Position = 0)]
        $Identity
    )
    $strFilter = "(&(objectCategory=User)(sAMAccountName=$Identity))"
    $objDomain = New-Object System.DirectoryServices.DirectoryEntry
    $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $objDomain
    $objSearcher.PageSize = 1000
    $objSearcher.Filter = $strFilter
    $objSearcher.SearchScope = "Subtree"
    $colProplist = "name", "mail", "memberof", "telephoneNumber", "homePhone", "title", "department", "Info", "pager"
    foreach ($i in $colPropList) {
        $objSearcher.PropertiesToLoad.Add($i) | Out-Null
    }
    $colResults = $objSearcher.FindAll()
    return $colResults
}

To get your users infos with the script above type: Get-ADUserInfos -Identity $env:username

If you need other properties edit the highlighted line in the script.

Leave a Reply

Powered by WordPress.com.

Up ↑

%d bloggers like this: