Use the Dsquery Computer command to find computers in the active directory that matches the specified search criteria.
To use the dsquery computer command, open the command prompt with Run as administrator to run the dsquery command.
In this article, we will discuss how to find computers in the directory and list the computers in the OU using the dsquery computer command.
Dsquery Computer
Syntax
dsquery computer [{<StartNode> | forestroot | domainroot}] [-o {dn | rdn | samid}] [-scope {subtree | onelevel | base}] [-name <Name>] [-desc <Description>] [-samid <SAMName>] [-inactive <NumberOfWeeks>] [-stalepwd <NumberOfDays>] [-disabled] [{-s <Server> | -d <Domain>}] [-u <UserName>] [-p {<Password> | *}] [-q] [-r] [-gc] [-limit <NumberOfObjects>] [{-uc | -uco | -uci}]
Dsquery Computer – Find Computers in AD
Use the dsquery computer command in the command prompt to find the computers in the active directory.
dsquery computer
The output of the above dsquery computer to list all the computers in the Active directory is:
List Computers in OU using cmd
To list the computers in OU (Organizational Unit), use the dsquery computer command and specify the OU’s distinguished name.
dsquery computer OU=SALES,DC=SHELLPRO,DC=LOCAL
In the above command, the dsquery computer command specified the OU path to find the computers in OU.
The output of the above command is:
C:\>dsquery computer OU=SALES,DC=SHELLPRO,DC=LOCAL
"CN=OPER-2,OU=SALES,DC=SHELLPRO,DC=LOCAL"
"CN=OPER-3,OU=SALES,DC=SHELLPRO,DC=LOCAL"
"CN=CORP-IT-E12,OU=SALES,DC=SHELLPRO,DC=LOCAL"
Using Dsquery to Find Inactive Computers
To find the inactive computers in the active directory, use the dsquery computer command. It has the inactive parameter that takes a value number of weeks.
dsquery computer -inactive 10 -limit 10
In the above command, the dsquery computer command uses the -inactive parameter to search for the computer not active for the specified 10 weeks and returns the list of inactive computers.
The output of the command to find inactive computers is:
C:\>dsquery computer -inactive 10 -limit 10
"CN=ENGG-PRO,OU=Domain Controllers,DC=SHELLPRO,DC=LOCAL"
Cool Tip: How to find all domain controllers using the dsquery server in cmd!
Find Computers using Dsquery Computer Name
The dsquery computer command has a name parameter to search for the computers in the active directory whose name attribute matches the specified computer name.
dsquery computer -name COR*
In the above command, dsquery computer command uses the name parameter to search for the computer name that starts with COR* and lists all the computers.
The output of the above command to find computers using the dsquery computer name is:
C:\>dsquery computer -name COR*
"CN=CORP-IT-E12,OU=SALES,DC=SHELLPRO,DC=LOCAL"
C:\>
Dsquery Computer Description – Find Computers
To find the computers in the active directory where the description attribute of the computer matches with the specified description, run the dsquery computer command.
dsquery computer -desc Zone*
In the above command, the dsquery computer uses the desc parameter to specify the computer description and search for the computer where the computer description attribute value matches the specified value.
The output of the above command to find computers in AD based on dsquery computer description is:
C:\>dsquery computer -desc Zone*
"CN=ENGG-PRO,OU=Domain Controllers,DC=SHELLPRO,DC=LOCAL"
"CN=OPER-01,CN=Computers,DC=SHELLPRO,DC=LOCAL"
"CN=OPER-2,OU=SALES,DC=SHELLPRO,DC=LOCAL"
"CN=OPER-3,OU=SALES,DC=SHELLPRO,DC=LOCAL"
C:\>
Find Computer Operating System using Dsquery
Using the dsquery command with filter parameter to search for the computers, it can find the computer operating system and computer attributes.
dsquery * "OU=SALES,DC=SHELLPRO,DC=LOCAL" -attr "cn" "operatingsystem" -filter "(&(objectclass=computer))"
In the above command, the dsquery command uses the filter parameter to search for the computers in AD in the specified OU and retrieves the computer name and operating system.
The output of the above command to get computer operating system using dsquery is:
C:\>dsquery * "OU=SALES,DC=SHELLPRO,DC=LOCAL" -attr "cn" "operatingsystem" -filter "(&(objectclass=computer))"
cn operatingsystem
OPER-2 Windows 10
OPER-3 Windows 10
CORP-IT-E12 Windows Server 2019
C:\>
Conclusion
I hope the above article on how to use the dsquery computer command to find the computer, and list all the computers in OU is helpful to you.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.