How to Get the Number of Monitors in Batch Script
Knowing how many monitors are connected to a system is a useful piece of information for diagnostic scripts, remote support, or for configuring applications that can leverage multiple displays. While there is no simple environment variable like %MONITOR_COUNT%, this information can be easily and reliably retrieved by counting the results from a WMIC (Windows Management Instrumentation Command-line) query.
This guide will teach you how to use WMIC to list all connected monitors and how to pipe that output to a counter to get a simple number. You will learn the correct way to parse this information in a batch script to capture the final count into a variable.
The Core Logic: List and Count
The strategy for getting the number of monitors is simple:
- Use a command that lists one line of output for each connected monitor.
- Count the number of lines in that output.
The WMIC utility is the perfect tool for the first step, and a FOR /F loop or a FIND command can handle the second.
The Core Command: WMIC PATH Win32_DesktopMonitor
The WMIC command is the standard way to query hardware information. The Win32_DesktopMonitor WMI class represents the physical displays connected to the computer. When we ask this class for a property, it will return one line of data for each monitor it detects.
Command to List Monitors: WMIC PATH Win32_DesktopMonitor GET Name
Example of output on a Dual-Monitor System. This output has three lines: a header ("Name") and one line for each of the two monitors. Our goal is to count just the data lines.
Name
Generic PnP Monitor
DELL U2718Q
Basic Example: A Simple Monitor Count
This command pipeline will directly output the number of connected monitors.
@ECHO OFF
ECHO --- Counting Connected Monitors ---
ECHO.
REM The find command will count the lines, but this includes the header.
WMIC PATH Win32_DesktopMonitor GET Name | find /c /v ""
Output:
--- Counting Connected Monitors ---
3
This result is off by one because it includes the header line. This is a common issue that we must handle correctly in a script.
How to Capture the Count in a Variable
The most robust and accurate way to get the count is to use a FOR /F loop. This allows us to easily skip the header and increment a counter for each data line found.
@ECHO OFF
SET "MonitorCount=0"
ECHO --- Capturing the Number of Monitors ---
REM 'skip=1' ignores the "Name" header line.
FOR /F "skip=1" %%M IN (
'WMIC PATH Win32_DesktopMonitor GET Name'
) DO (
SET /A "MonitorCount+=1"
)
ECHO.
ECHO The number of detected monitors is: %MonitorCount%
This script will correctly report 2 for the dual-monitor example.
How the script works:
WMIC PATH Win32_DesktopMonitor GET Name: This command queries the WMI service for all instances of theWin32_DesktopMonitorclass and asks for theNameproperty of each. The WMI service gets this information from the operating system's Plug and Play manager.FOR /F "skip=1" %%M IN ('...'): TheFOR /Floop executes theWMICcommand and begins to process its output.skip=1: This crucial option tells the loop to ignore the first line of the command's output, which is the header (Name).DO (SET /A "MonitorCount+=1"): For every subsequent line of output (one for each monitor), theDOblock is executed, and our counter is incremented.
When the loop finishes, MonitorCount holds the correct number of monitors.
Common Pitfalls and How to Solve Them
Problem: The Header Line is Counted by Mistake
This is the most common error, which happens when you use a simple pipe to find /c /v "".
Solution: The FOR /F "skip=1" loop with a counter is the definitive solution to this problem. It is the most reliable way to ignore the header and get an accurate count.
Problem: "Phantom" or Virtual Monitors
Sometimes, the count might be higher than the number of physical monitors you can see. This can be caused by:
- Virtual Machine Software: Hyper-V, VMware, or other virtualization software can create virtual display devices.
- Remote Desktop Software: Tools like LogMeIn or TeamViewer can install a "mirror driver" that appears as a monitor.
- "Headless" Systems: Some servers that run without a physical monitor might still report a basic, generic display adapter.
Solution: This is expected behavior, as WMIC reports on all devices known to Windows. Your script is reporting the correct number of logical monitors. There is no simple way to distinguish between physical and virtual monitors with WMIC alone.
Practical Example: A "Multi-Monitor Check" Script
This script checks if more than one monitor is connected and displays a different message depending on the result. This could be a prerequisite check for a program that requires a multi-monitor setup.
@ECHO OFF
SETLOCAL
TITLE Display Configuration Check
SET "MonitorCount=0"
FOR /F "skip=1" %%M IN ('WMIC PATH Win32_DesktopMonitor GET Name') DO (
SET /A "MonitorCount+=1"
)
ECHO --- Display Setup Analysis ---
ECHO.
ECHO Found %MonitorCount% monitor(s) connected to this system.
ECHO.
IF %MonitorCount% GTR 1 (
ECHO [INFO] A multi-monitor setup was detected.
) ELSE (
ECHO [INFO] A single-monitor setup was detected.
)
ENDLOCAL
Conclusion
The WMIC command is the standard, built-in tool for retrieving hardware information, including a list of connected monitors.
Key takeaways for getting an accurate count:
- Use
WMIC PATH Win32_DesktopMonitor GET Nameto generate a list with one line per monitor. - The most reliable way to count this list is with a
FOR /F "skip=1"loop and a counter variable. This correctly handles the header line. - Be aware that the count will include all logical monitors known to Windows, including virtual ones.