How to List All Computers in an Active Directory Organizational Unit in Batch Script
Organizational Units (OUs) are the logical folders where you group your company's technology assets. For IT administrators, being able to "List" every computer within a specific OU is a fundamental requirement for system inventory, bulk software deployments, and security auditing. If you need to know which 50 PCs are currently sitting in the "Marketing" branch of your directory, using a Batch script is the fastest way to pull that roster.
This guide explains how to use the dsquery utility to extract a list of computers from any OU.
Why List Computers by OU?
- Asset Management: generating a quick text or CSV report of every workstation in a specific department for an annual hardware audit.
- Maintenance Automation: using your script to find every computer in a "Servers" OU and then automatically triggering a ping test or a remote reboot.
- Deployment Preparation: Verifying the presence of all machines in a "Staging" OU before initiating a wide-scale Windows Update or software push.
The dsquery command is part of the Remote Server Administration Tools (RSAT). It must be installed on your workstation to perform this lookup against your domain.
Method 1: Using DSQUERY (The Standard way)
The dsquery computer command allows you to specify a starting point (the OU's Distinguished Name) and list every machine object under that path.
@echo off
set "OU_DN=OU=Workstations,DC=Contoso,DC=com"
echo [PROCESS] auditing computer objects in: %OU_DN%
:: The output will be the Distinguished Name of every computer found
dsquery computer "%OU_DN%"
if %errorlevel% neq 0 (
echo [ERROR] OU not found or no computer objects are present.
)
pause
Method 2: Extracting Simple Computer names
By default, dsquery returns the full "Distinguished Name" (DN). If you just want the simple names (e.g., PC-01) for a report, you can pipe the results to dsget.
@echo off
set "TARGET_OU=OU=Laptops,DC=Contoso,DC=com"
echo [PROCESS] Generating simplified machine roster...
echo.
:: We pipe the found computer objects to dsget to extract just the -samid
dsquery computer "%TARGET_OU%" | dsget computer -samid
pause
Creating a Reachability Health Checker
This professional script finds every computer in an OU and performs a quick "Ping test" on each, perfect for identifying which machines are currently online.
@echo off
setlocal
echo ============================================================
echo Active Directory OU Reachability Scanner
echo ============================================================
set /p "OU=Enter OU DN (e.g., OU=Accounting,DC=Lab,DC=local): "
:: 1. Verify Connectivity
echo [PROCESS] searching and testing machines...
echo.
for /f "tokens=*" %%c in ('dsquery computer "%OU%" 2^>nul ^| dsget computer -samid ^| findstr /V "samid"') do (
set "PC=%%c"
:: Trim trailing '$' from samid to get the clean hostname
set "PC=!PC:$=!"
ping -n 1 !PC! >nul 2>&1
if errorlevel 1 (
echo [OFFLINE] !PC!
) else (
echo [ONLINE ] !PC!
)
)
echo.
echo ============================================================
pause
Common Pitfalls and How to Avoid Them
Distinguished Name (DN) Typos
The dsquery command is extremely sensitive to commas and spaces in the OU path.
Wrong Way:
dsquery computer "OU=Workstations, DC=Contoso, DC=com"
:: Note the extra spaces after commas; this will fail.
Correct Way:
Always use the exact path: OU=Workstations,DC=Contoso,DC=com.
Advise your users that if they are unsure of the OU's path, they should first run dsquery ou -name "Marketing" to get the correct DN before trying to query computers within it.
Best Practices for OU Auditing
- Check for "Stale" Computers: many machines found in your OU list might have been decommissioned years ago but their objects still exist in AD. Cross-reference your results with a "Last Logon" audit.
- Include Sub-OUs: by default,
dsquerysearches the specified OU and all of its sub-containers. If you only want objects in the top level, add the-scope baseflag. - Export to CSV: use your script to create a permanent inventory record:
dsquery computer "%OU%" | dsget computer -samid -desc > inventory.csv.
To query Active Directory computers, your computer must be joined to the domain and search permissions must be granted to the account running the script (usually standard "Domain User" rights are sufficient for reading machine lists).
Conclusion
Listing all computers in an Organizational Unit via Batch script is a critical skill for maintaining a professional and well-managed enterprise network. By leveraging the dsquery and dsget utilities to programmatically extract machine rosters, you can automate inventory collection, simplify maintenance tasks, and improve overall system reachability. This professional approach to system identification ensure that your organization's technology assets are always accurately tracked and managed, providing a reliable and automated solution for handling organizational growth across the entire Windows domain.