Blog Post

Find AD user detail - PowerShell Script

,

I developed the below script to find the details of a AD user and the AD groups he is associated with, Script 1 is user search by AD user ID and Script 2 is search by AD user name(Last or First Name)

Script 1:- 

#

# Script to Find AD User Details
# Created by - Vinoth N Manoharan
# Version 1.1
# Date - 15/09/2011
Please Enter $usr variable with AD user ID you want to Search

Clear-Host
$usr = "Ad user ID"
$objItem = @()
$strFilter = "(&(objectCategory=User)(name=$usr))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
#$objDomain |Get-Member
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
#$objSearcher | Get-Member
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
#$colProplist = "name"
#foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i);}
$colResults = $objSearcher.FindAll()
#$colResults|Get-Member
foreach ($objResult in $colResults)
{
$objItem = $objResult.GetDirectoryEntry()
#$objItem|Get-Member
"`n"
"Name: " + $objItem.givenName
"Display Name: " + $objItem.displayName
"Account Name: " + $objItem.sAMAccountName
"Department: " + $objItem.department
"TelephoneNumber: " + $objItem.telephoneNumber
"Member Of:"
"----------"
foreach($membergrp in $objItem.memberOf)
{
$objuser1 = New-Object System.DirectoryServices.DirectoryEntry("LDAP://"+$membergrp)
"`t"+$objuser1.name
}
#Member Of: `n" +$objItem.memberOf
#$objItem.displayName
}

Script 2:- 

#
# Script to Find AD User Details
# Created by - Vinoth N Manoharan
# Version 1.1
# Date - 15/09/2011
#Please Enter $usr variable with AD user name you want to Search

Clear-Host
$usr = "Vinoth"
$objItem = @()
$strFilter = "(&(objectCategory=User)(|(givenName=*$usr*)(sn=*$usr*)(name=*$usr*)))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
#$objDomain |Get-Member
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
#$objSearcher | Get-Member
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
#$colProplist = "name"
#foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i);}
$colResults = $objSearcher.FindAll()
#$colResults|Get-Member
foreach ($objResult in $colResults)
{
$objItem = $objResult.GetDirectoryEntry()
#$objItem|Get-Member
"`n"
"Name: " + $objItem.givenName
"Surname: " + $objItem.sn
"Display Name: " + $objItem.displayName
"Account Name: " + $objItem.sAMAccountName
"Department: " + $objItem.department
"TelephoneNumber: " + $objItem.telephoneNumber
"Member Of:"
"----------"
foreach($membergrp in $objItem.memberOf)
{
$objuser1 = New-Object System.DirectoryServices.DirectoryEntry("LDAP://"+$membergrp)
"`t"+$objuser1.name
}
#$objItem.memberOf
#$objItem.displayName
}

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating