I created this function because sometimes you don’t have the Active Directory PowerShell module installed on your system. This function works on various Operating Systems such as Windows 7, 8.x, 10 and also on Server 2008 and higher. It is ideal to include in login scripts or startup scripts to gather information about the current user that is logging into the system.
Examples:
In the below examples certain values are blurred because these are taken from a live environment.
Code:
Function get-ADSIUserInfo {
<#
.SYNOPSIS
Gathers user information using ADSI from the current domain on which this is executed unless explicitly specified.
.DESCRIPTION
Returns all information which is filled in or default stored on the user object in Active Directory
.EXAMPLE
get-ADSIUserInfo -UserAccount marcmuld
.PARAMETER UserAccount
The user account name to search for (samaccountname) in Active Directory
#>
Param (
[Parameter(Mandatory = $True)]
[string]$UserAccount
)
$ADSISearcher = New-Object DirectoryServices.DirectorySearcher
$ADSISearcher.Filter = "(&(objectCategory=person)(objectClass=User)(samaccountname=$($UserAccount)))"
$ADSIResults = $ADSISearcher.findone()
return $ADSIResults
}