Get-AD.ps1
This Script gets Information About Objects in Active-Directory. It’s got a few Parameters and Switches that allows us to specify what to search for and how to Return the objects found. You can choose which information to return through the Property parameter, you can Save the Information to a Csv file, You can return a System.DirectoryServices.DirectoryEntry Object that you can work with through PowerShell.. you can do lots of things.
Parameters:
- -Domain Name of the Domain (Required)
- -OU Name of Organizational Unit (Optional)
- -User Name of the User (Optional)
- -Group Name of the Group (Optional)
- -Computer Name of the Computer (Optional)
- -Filter Filter on Specified Criteria, default is name (optional)
- -CustomFilter Create A custom SearchFilter (optional)
- -CustomAll Create A Custom SerachFilter, searches for All Objects (optional)
- -Property Specify one or more Properties to Return (Optional)
- -ToCsv Saves the Output to a Csv File (Optional)
- -ToObject Returns a System.DirectoryServices.DirectoryEntry Object (optional)
- -IncreasePageSize Exceeds the default limit of 1000 Objects (optional)
- -help Prints the HelpFile (Optional)
PS > Get-AD.ps1 -HelpYou want to Display Domain Information About Your Domain
PS > Get-AD.ps1 -Domain apa.corpYou want to Connect to a Domain and store the Object in a Variable
PS > $Domain = Get-AD.ps1 -Domain apa.corp -ToObjectYou want to retrieve All OrganizationalUnits in your Domain and store their distiguishedName in a Csv file
PS > Get-AD.ps1 -Domain apa.corp -OU AllOU -Property distinguishedName -ToCsv C:MyFolderMyOUFile.csvYou want to Get all Users and display their name and mail in your PowerShell Session
PS > Get-AD.ps1 -Domain apa.corp -User AllUsers -Property cn, mailYou want to Find a User, but you only know the Users sAMAccountName
PS > Get-AD.ps1 -Domain apa.corp -User User1 -Filter sAMAccountName -Property cn, mailYou want to Create a Csv File with all Computers in your domain
PS > Get-AD.ps1 -Domain apa.corp -Computer AllCOmputers -Property Name -ToCsv C:\MyFolderMyOUFile.csvYou can Download the Script here
[?]


Very interesting read and nice script, but I have a problem running it.
I get the error
You cannot call a method on a null-valued expression.
At E:\PowershellScripts\get-ad.ps1:631 char:59
+ $SearchResult = ($Searcher.FindOne()).GetDirectoryEntry <<<< ()
+ CategoryInfo : InvalidOperation: (GetDirectoryEntry:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Can you give me some help?
Oh sorry, I meant to post this earlier. I was running the script in this fashion.
$User1 = .\Get-AD.ps1 -Domain Domain -User rbentancur -ToObject
That is when I got the error above.
Hi, use the -Filter parameter to specify what you want to filter on; sn, givenName, sAMAccountName etc. (think its default set to cn).
here’s an example:
PS > $user = .\Get-AD.ps1 -Domain powershell.nu -User nigo -Filter sAMAccountName -ToObject
PS > $user
distinguishedName : {CN=Niklas Goude,CN=Users,DC=POWERSHELL,DC=NU}
Path : LDAP://CN=Niklas Goude,CN=Users,DC=POWERSHELL,DC=NU
I have only been messing with the Get-AD.ps1’s ability to find computers in AD but when I do a search for a computer name not found in the domain I get the following error:
PS C:\> .\get-ad.ps1 -Domain “mydomain” -Computer “ComputerName”
You cannot call a method on a null-valued expression.
At C:\get-ad.ps1:630 char:59
+ $SearchResult = ($Searcher.FindOne()).GetDirectoryEntry <<<< ()
+ CategoryInfo : InvalidOperation: (GetDirectoryEntry:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
I know the error is here but wanted to put in an if statement that sets variable to “No object found” and displays it to host.
Code snippets
# Collect Information through DirectorySearcher
$Searcher.Filter = $SearchFilter
$SearchResult = ($Searcher.FindOne()).GetDirectoryEntry() <this is where it happens but do not know how to check if method is null.
It also would pop 3 other errors in addition to the one posted above but I was able to add
# Return Information to Host
if ($Property -eq "AllProperties") {
$ObjectPropertyNames | ForEach {
$Name = $_
if ($Name -eq $null) {} else { #Jb edits to keep the errors down
$Name = $Name.ToString()
$Value = $ObjectProperties[$Name]
$Value = $Value.ToString()
if($Value -eq $Null) {
$Value = "Unknown"
}
$ADObject | Add-Member -memberType NoteProperty $Name -Value $Value
}}
Thank you
I am getting the same error as listed above when trying to use this to get a listing of all computers via OU by name and description. I have tried the recommendations above but it is not working… the command I am running is this:
c:\Scripts\Get-AD.ps1 -Domain OU.domain.com -User AllGroups -Property name, description -ToCsv c:\scripts\list.csv
Do you have any recommendations?
Well that fixed itself when I closed and then reopened PowerShell.
lol
One other question… any recommendations on how to search for computers in an OU in a domain without searching other OUs? I have tried:
-Domain test.com OU group1 -computers AllComputers -ToCsv test.csv
but all it returns is the OU information. I have also tried to run the following:
-Domain test.com -computers AllComputers -CustomAll “(&(ObjectCategory=OU)(OU=group1))”
This too returns values on all objects in the domain, not just the OU.
Hi Niklas,
Very nice script, just what I was looking for. I just have a small issue, not really an issue though… When searching computeraccounts in AD it gives me only 1000 records, is it possible to counter this limit?
Regards
Another question…
Is it possible to query all computers in the domain and receive as result not only their Computernames but also their Operating system, service pack and eventually the OU where they belong?
Regards!