PowerShell Script: Finding A Distinguished Name of a Group/User: Function Find-DN
By: Brenton Blawat
This article explains the the method by which one would be able to Search Active Directory for the distinguished name of a User or Group. This is helpful when trying to add an object to Active Directory or adding Users to Groups.
Function Find-DN Finding Distinguished Name: Download PS1 Here
1: # Function Find Distinguished Name
2: function find-dn { param([string]$adfindtype, [string]$cName)
3: # Create A New ADSI Call
4: $root = [ADSI]''
5: # Create a New DirectorySearcher Object
6: $searcher = new-object System.DirectoryServices.DirectorySearcher($root)
7: # Set the filter to search for a specific CNAME
8: $searcher.filter = "(&(objectClass=$adfindtype) (CN=$cName))"
9: # Set results in $adfind variable
10: $adfind = $searcher.findall()
11:
12: # If Search has Multiple Answers
13: if ($adfind.count -gt 1) {
14: $count = 0
15: foreach($i in $adfind)
16: {
17: # Write Answers On Screen
18: write-host $count ": " $i.path
19: $count += 1
20: }
21: # Prompt User For Selection
22: $selection = Read-Host "Please select item: "
23: # Return the Selection
24: return $adfind[$selection].path
25: }
26: # Return The Answer
27: return $adfind[0].path
28: }
Using this Function:
1: # To use the function to find a User:
2: find-dn "user" "Blawat"
3:
4: # To use the function to find a Group:
5: find-dn "group" "IT Help Desk"
Read Full Post | Make a Comment ( None so far )


