Monday, 13 August 2012

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:-

1:  #  
2:  # Script to Find AD User Details  
3:  # Created by - Vinoth N Manoharan  
4:  # Version 1.1  
5:  # Date - 15/09/2011  
6:  # Please Enter $usr variable with AD user ID you want to Search  
7:  Clear-Host  
8:  $usr = "Ad user ID"  
9:  $objItem = @()  
10:  $strFilter = "(&(objectCategory=User)(name=$usr))"  
11:  $objDomain = New-Object System.DirectoryServices.DirectoryEntry  
12:  #$objDomain |Get-Member  
13:  $objSearcher = New-Object System.DirectoryServices.DirectorySearcher  
14:  #$objSearcher | Get-Member  
15:  $objSearcher.SearchRoot = $objDomain  
16:  $objSearcher.PageSize = 1000  
17:  $objSearcher.Filter = $strFilter  
18:  $objSearcher.SearchScope = "Subtree"  
19:  #$colProplist = "name"  
20:  #foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i);}  
21:  $colResults = $objSearcher.FindAll()  
22:  #$colResults|Get-Member  
23:  foreach ($objResult in $colResults)  
24:  {  
25:  $objItem = $objResult.GetDirectoryEntry()  
26:  #$objItem|Get-Member  
27:  "`n"  
28:  "Name: " + $objItem.givenName  
29:  "Display Name: " + $objItem.displayName  
30:  "Account Name: " + $objItem.sAMAccountName  
31:  "Department: " + $objItem.department  
32:  "TelephoneNumber: " + $objItem.telephoneNumber  
33:  "Member Of:"  
34:  "----------"  
35:  foreach($membergrp in $objItem.memberOf)  
36:  {  
37:  $objuser1 = New-Object System.DirectoryServices.DirectoryEntry("LDAP://"+$membergrp)  
38:  "`t"+$objuser1.name  
39:  }  
40:  #Member Of: `n" +$objItem.memberOf  
41:  #$objItem.displayName  
42:  }  

Script 2:-

1:  #  
2:  # Script to Find AD User Details  
3:  # Created by - Vinoth N Manoharan  
4:  # Version 1.1  
5:  # Date - 15/09/2011  
6:  ## Please Enter $usr variable with AD user name you want to Search  
7:  Clear-Host  
8:  $usr = "Vinoth"  
9:  $objItem = @()  
10:  $strFilter = "(&(objectCategory=User)(|(givenName=*$usr*)(sn=*$usr*)(name=*$usr*)))"  
11:  $objDomain = New-Object System.DirectoryServices.DirectoryEntry  
12:  #$objDomain |Get-Member  
13:  $objSearcher = New-Object System.DirectoryServices.DirectorySearcher  
14:  #$objSearcher | Get-Member  
15:  $objSearcher.SearchRoot = $objDomain  
16:  $objSearcher.PageSize = 1000  
17:  $objSearcher.Filter = $strFilter  
18:  $objSearcher.SearchScope = "Subtree"  
19:  #$colProplist = "name"  
20:  #foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i);}  
21:  $colResults = $objSearcher.FindAll()  
22:  #$colResults|Get-Member  
23:  foreach ($objResult in $colResults)  
24:  {  
25:  $objItem = $objResult.GetDirectoryEntry()  
26:  #$objItem|Get-Member  
27:  "`n"  
28:  "Name: " + $objItem.givenName  
29:  "Surname: " + $objItem.sn  
30:  "Display Name: " + $objItem.displayName  
31:  "Account Name: " + $objItem.sAMAccountName  
32:  "Department: " + $objItem.department  
33:  "TelephoneNumber: " + $objItem.telephoneNumber  
34:  "Member Of:"  
35:  "----------"  
36:  foreach($membergrp in $objItem.memberOf)  
37:  {  
38:  $objuser1 = New-Object System.DirectoryServices.DirectoryEntry("LDAP://"+$membergrp)  
39:  "`t"+$objuser1.name  
40:  }  
41:  #$objItem.memberOf  
42:  #$objItem.displayName  
43:  }  

Copyright © 2012 Vinoth N Manoharan.The information provided in this post is provided "as is" with no implied warranties or guarantees.

No comments:

Post a Comment