Description

This script is similar to the one found in http://gallery.technet.microsoft.com/scriptcenter/1a7111e3-3c15-4e29-ac3b-84d3ac46bd4c but in PowerShell.

It basically returns the DN for a user account if found in your Active Directory domain. It is useful as you can easily copy the function and incorporate it in some other script.

 

Script

PowerShell
Edit|Remove
# Get the user's Distinguished Name 
Function Get-DistinguishedName ($strUserName) 
{  
   $searcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]'') 
   $searcher.Filter = "(&(objectClass=User)(samAccountName=$strUserName))" 
   $result = $searcher.FindOne() 
 
   Return $result.GetDirectoryEntry().DistinguishedName 
} 
 
 
# Check if the user specified a useraccount 
If ($args[0] -eq $null) 
{ 
    $strUserName = Read-Host "Please enter the user's account name (e.g. nmota)" 
} 
Else 
{ 
    $strUserName = $args[0] 
} 
 
$strDN = Get-DistinguishedName $strUserName 
$strDN