Skip to main content

How to Get the Current Screen Resolution in Batch Script

Knowing the screen resolution of a system can be useful for diagnostic scripts, remote support tools, or for configuring applications that need to adjust their behavior based on the display size. While there is no simple environment variable like %RESOLUTION%, this information is readily available through the Windows Management Instrumentation (WMI) framework.

This guide will teach you how to use the built-in WMIC (Windows Management Instrumentation Command-line) utility to query the current vertical and horizontal screen resolution. You will learn the simple command to display the values and the standard scripting pattern to capture them into separate variables.

The Core Command: WMIC DESKTOPMONITOR GET

The standard way to get display information from the command line is with WMIC. The specific WMI class that holds the screen resolution is Win32_DesktopMonitor.

The command to get the width and height is: WMIC PATH Win32_DesktopMonitor GET ScreenHeight, ScreenWidth

  • WMIC: The command-line utility.
  • PATH Win32_DesktopMonitor: Specifies the WMI class we are querying.
  • GET ScreenHeight, ScreenWidth: The specific properties we want to retrieve.

Key Resolution Properties Explained

These properties represent the current resolution of the primary display.

  • ScreenWidth: The width of the screen in pixels (e.g., 1920).
  • ScreenHeight: The height of the screen in pixels (e.g., 1080).

Basic Example: Displaying the Current Resolution

You can run this command directly in a command prompt to see the resolution of your current machine.

@ECHO OFF
ECHO --- Querying Screen Resolution ---
ECHO.
WMIC PATH Win32_DesktopMonitor GET ScreenHeight, ScreenWidth

The output from WMIC is formatted in columns with a header and the values on the next line.

--- Querying Screen Resolution ---

ScreenHeight ScreenWidth
1080 1920

How to Capture the Resolution in Variables

To use the height and width in a script, you need to capture them into separate variables. The standard FOR /F loop is the perfect tool for parsing the WMIC output.

@ECHO OFF
SET "ScreenHeight="
SET "ScreenWidth="

ECHO --- Capturing Screen Resolution ---

REM 'skip=1' ignores the header line. 'tokens=1,2' grabs the two columns.
FOR /F "skip=1 tokens=1,2" %%A IN (
'WMIC PATH Win32_DesktopMonitor GET ScreenHeight, ScreenWidth'
) DO (
SET "ScreenHeight=%%A"
SET "ScreenWidth=%%B"
GOTO :ResolutionCaptured
)

:ResolutionCaptured
IF NOT DEFINED ScreenWidth (
ECHO [FAILURE] Could not retrieve the screen resolution.
GOTO :EOF
)

ECHO.
ECHO The captured resolution is: %ScreenWidth% x %ScreenHeight%
note
  • skip=1: This tells the FOR loop to ignore the first line of output (the ScreenHeight ScreenWidth header).
  • tokens=1,2: This specifies that we want to capture the first and second "words" (columns) of the output. The first (1080) goes into %%A, and the second (1920) goes into %%B.
  • GOTO :ResolutionCaptured: This exits the loop immediately after the first line of data is captured, preventing the variables from being overwritten by blank lines.

How the Method Works

The WMIC command is an interface to the WMI service, which is a core part of Windows that manages data and operations for the operating system. The Win32_DesktopMonitor class is a standardized object that WMI uses to represent a physical display. This object contains many properties, and ScreenHeight and ScreenWidth are two of the most useful. Our command simply asks WMI to query the current state of that object and return the values of those properties.

Common Pitfalls and How to Solve Them

Problem: The System Has Multiple Monitors

If a system has more than one monitor, the WMIC command will return a line of output for each monitor.

Example Output on a Dual-Monitor System:

ScreenHeight  ScreenWidth
1080 1920
1440 2560

Solution: The simple script provided will only capture the last monitor in the list. For many use cases, this is sufficient. If you need to know the resolution of the primary monitor specifically, the WMI query becomes more complex (Win32_VideoController is needed), and a PowerShell script is often a better tool for that advanced scenario.

Problem: Parsing the WMIC Output is Tricky

The WMIC command's output, with its header, trailing spaces, and extra blank lines, can be difficult to parse reliably.

Solution: The FOR /F "skip=1 tokens=1,2" loop combined with an immediate GOTO is the most robust and recommended pattern for capturing this specific WMIC data. It cleanly isolates the first line of data and ignores the rest of the noise.

Practical Example: A Simple Display Information Report

This script gathers several key pieces of display information and saves them to a report file.

@ECHO OFF
SETLOCAL
SET "REPORT_FILE=%USERPROFILE%\Desktop\Display_Info.txt"

ECHO --- Display Information Reporter ---
ECHO Creating report at: "%REPORT_FILE%"

(
ECHO Display Report for: %COMPUTERNAME%
ECHO Generated on: %DATE% @ %TIME%
ECHO ------------------------------------
ECHO.

REM Get Display Resolution
FOR /F "skip=1 tokens=1,2" %%A IN ('WMIC PATH Win32_DesktopMonitor GET ScreenHeight, ScreenWidth') DO (
ECHO Resolution: %%B x %%A
GOTO :GotResolution
)
:GotResolution

REM Get Video Card Name
FOR /F "skip=1 delims=" %%C IN ('WMIC PATH Win32_VideoController GET Caption') DO (
ECHO Video Card: %%C
GOTO :GotCard
)
:GotCard

) > "%REPORT_FILE%"

ECHO.
ECHO --- Report Complete ---
START "" "%REPORT_FILE%"
ENDLOCAL

Conclusion

The WMIC PATH Win32_DesktopMonitor command is the standard, built-in, and most reliable method for retrieving the screen resolution from a batch script.

Key takeaways:

  • Use WMIC PATH Win32_DesktopMonitor GET ScreenHeight, ScreenWidth to query the resolution.
  • Use a FOR /F "skip=1 tokens=1,2" loop with a GOTO to capture the height and width into separate variables.
  • Be aware that this command will report on all connected monitors, and the simple script will capture the last one in the list.
  • This method requires no external tools and works on all modern Windows systems.