PoshCode Logo PowerShell Code Repository

Get-User by Jonathan Walz 3 years ago
View followups from spadm, EDV-Buero, dkx884s, fabian and fabian | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/628"></script>download | new post

This function is an attempt to duplicate the Quest Get-QADUser cmdlet without using any third party snap-ins. If you want to run it against a Global Catalog you simply need to replace LDAP: with GC: and you will want to comment out the lines that pull the password last set and last logon timestamp unless you happen to be replicating those to your GC.

  1. function Get-User($user)
  2. {
  3.         # this function should be passed the CN of the user to be returned
  4.         $dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
  5.         $root = [ADSI] "LDAP://$($dom.Name)"
  6.         $searcher = New-Object System.DirectoryServices.DirectorySearcher $root
  7.         $searcher.filter = "(&(objectCategory=person)(objectClass=user)(cn=$user))"
  8.         $user = $searcher.FindOne()
  9.         [System.Collections.Arraylist]$names = $user.Properties.PropertyNames
  10.         [System.Collections.Arraylist]$props = $user.Properties.Values
  11.         $userobj = New-Object System.Object
  12.         for ($i = 0; $i -lt $names.Count)
  13.                 {
  14.                         $userobj | Add-Member -type NoteProperty -Name $($names[$i]) -Value $($props[$i])
  15.                         $i++
  16.                 }
  17.         $userobj.pwdlastset = [System.DateTime]::FromFileTime($userobj.pwdlastset)
  18.         $userobj.lastlogontimestamp = [System.DateTime]::FromFileTime($userobj.lastlogontimestamp)
  19.         return $userobj
  20. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me