Get display resolution from windows command line

19

6

I have seen several suggestions about programs to change the resolution from the command line. However I just want to display it, not change it.

On linux I can use xrandr or xdpyinfo to get this info, so I am looking for something like that.

I also need it to work within a cygwin shell.

Zitrax

Posted 2011-04-14T09:51:55.653

Reputation: 429

2

Note: There is also a PowerShell version available if anyone is interested. Over at StackOverflow they also solved the multimonitor problem

– nixda – 2015-06-07T07:21:45.777

Answers

20

Try this:

wmic desktopmonitor get screenheight, screenwidth

From within Cygwin:

cmd /c wmic desktopmonitor get screenheight, screenwidth

I'm not sure what tricks to use in order to use the output. Perhaps a temporary text file?

paradroid

Posted 2011-04-14T09:51:55.653

Reputation: 20 970

2Does not work on Win8.1 or win10. It gives empty results for both screenheight and screenwidth. – David Balažic – 2018-02-08T17:36:25.960

@DavidBalažic That's weird. Have you tried from an elevated prompt? – paradroid – 2018-02-08T17:39:20.713

1

@paradroid It does not work, as explained in the other answer

– David Balažic – 2018-02-09T11:43:41.337

This gives the max supported resolution of the display device, not the active resolution. – Pedro Lobito – 2019-05-14T23:17:47.910

Yes thanks it works from cmd.exe. However I forgot to mention that I need this to work inside a cygwin shell and wmic do not seem to work there. – Zitrax – 2011-04-15T09:30:41.257

1@Zitrax: Now you tell me. – paradroid – 2011-04-15T09:52:52.120

Thanks again. It did not work when connecting with rdesktop (or ssh into cygwin) though. Would be great to have it in all those situations also. – Zitrax – 2011-04-15T20:16:12.377

To me it doesn't work correctly with a multimonitor setup (it always returns the resolution of the 1st monitor, even if it is disabled) (My OS: WinXP). – eadmaster – 2013-11-19T06:22:34.820

10

With dxdiag though it is not the fastest way:

@echo off

del ~.txt /q /f >nul 2>nul
start "" /w dxdiag /t ~
setlocal enableDelayedExpansion
set currmon=1 
for /f "tokens=2 delims=:" %%a in ('find "Current Mode:" ~.txt') do (
    echo Monitor !currmon! : %%a
    set /a currmon=currmon+1

)
endlocal
del ~.txt /q /f >nul 2>nul

this will print the resolutions of all monitors.

Edit. The accepted answer uses WMIC. (wmic desktopmonitor get screenheight, screenwidth /format:value).This will not work on windows8/8.1/10. For the newer windows versions this can be used:

wmic path Win32_VideoController get VideoModeDescription,CurrentVerticalResolution,CurrentHorizontalResolution /format:value

A script that checks the windows version and then gets the resolution with the wmic:

@echo off

setlocal
for /f "tokens=4,5 delims=. " %%a in ('ver') do set "version=%%a%%b"


if version lss 62 (
    ::set "wmic_query=wmic desktopmonitor get screenheight, screenwidth /format:value"
    for /f "tokens=* delims=" %%@ in ('wmic desktopmonitor get screenwidth /format:value') do (
        for /f "tokens=2 delims==" %%# in ("%%@") do set "x=%%#"
    )
    for /f "tokens=* delims=" %%@ in ('wmic desktopmonitor get screenheight /format:value') do (
        for /f "tokens=2 delims==" %%# in ("%%@") do set "y=%%#"
    )

) else (
    ::wmic path Win32_VideoController get VideoModeDescription,CurrentVerticalResolution,CurrentHorizontalResolution /format:value
    for /f "tokens=* delims=" %%@ in ('wmic path Win32_VideoController get CurrentHorizontalResolution  /format:value') do (
        for /f "tokens=2 delims==" %%# in ("%%@") do set "x=%%#"
    )
    for /f "tokens=* delims=" %%@ in ('wmic path Win32_VideoController get CurrentVerticalResolution /format:value') do (
        for /f "tokens=2 delims==" %%# in ("%%@") do set "y=%%#"
    )

)

echo Resolution %x%x%y%

endlocal

npocmaka

Posted 2011-04-14T09:51:55.653

Reputation: 887

2

Thank you @paradroid :) With WMIC, I wrote Batch Script to Remote Desktop not full screen but still convenient. ^_^

@echo off
:p00
setlocal
if "%1"=="" goto :q01
set i01=wmic desktopmonitor
set i01=%i01% where availability^=3
set i01=%i01% get screenHeight,screenWidth
set o01=%temp%\ScrRes.txt
%i01%>"%o01%"
for /f "delims= skip=1" %%o in ('type %o01%') do call :p01 %1 %%o
goto :p99

:p01
set srvnm=%1
set /a tl=%2-40
set /a ll=%3-80
start mstsc /admin /w:%ll% /h:%tl% /v:%srvnm%
goto :eof

:q01
echo.
echo ^>^> Syntax: %0 MachineHostname [enter]
echo.

:p99
if exist "%o01%" del "%o01%" /f /q
echo.
echo ^>^> Sincerely Thank You For Using..
endlocal
goto :eof

Feel free to explore. Feel enthusiast to enhance. (y)

Rhak Kahr

Posted 2011-04-14T09:51:55.653

Reputation: 191

1

The simplest way :

@echo off
::By SachaDee 2018

FOR /F "skip=2 delims=" %%a IN ('wmic path Win32_VideoController get VideoModeDescription^,CurrentHorizontalResolution^,CurrentVerticalResolution /format:Value ^| findstr ":"') do set %%a

echo Width =^> %CurrentHorizontalResolution%
echo Height =^> %CurrentVerticalResolution%
echo Description =^> %VideoModeDescription%

SachaDee

Posted 2011-04-14T09:51:55.653

Reputation: 111

Can you give a little more explanation into what's going on here? I understand /format:Value returns results in a var=value form which is then used in the set command. If you just want one value, is there a way to do this without the for loop? – Kyle Delaney – 2018-06-19T14:40:32.343

If you just want to display the needed value, you can for sure just run a wmic query alone with the right parameters to just display the value. The for loop is used here to set the values for a later use in the code. That's the only way to do it in bat using an external command (wmic.exe in this case). – SachaDee – 2018-06-19T21:14:28.437

You can't use set with a wmic result without a for loop? – Kyle Delaney – 2018-06-19T21:15:53.350

No its not possible ! – SachaDee – 2018-06-19T21:20:35.193

1

use MultiMonitorTool:

MultiMonitorTool.exe /scomma "%TEMP%\MultiMonitorTool.csv"

then parse the file "%TEMP%\MultiMonitorTool.csv" (i'm still working on this)

eadmaster

Posted 2011-04-14T09:51:55.653

Reputation: 706

1

the oldes answer does not seems to work anymore (win7 64bit); i solved that way

FOR /f "tokens=1,2" %%a IN ('"wmic desktopmonitor get screenheight, screenwidth"') DO (
    SET /a ScreenHeight=%%a
    SET /a ScreenWidth=%%b
)
echo %ScreenHeight%
echo %ScreenWidth%

Lesto

Posted 2011-04-14T09:51:55.653

Reputation: 121

1

For multi monitor setup just split the command:

setlocal ENABLEDELAYEDEXPANSION
setlocal ENABLEEXTENSIONS
set wmicheight="wmic desktopmonitor get screenheight /format:value"
set wmicwidth="wmic desktopmonitor get screenwidth /format:value"
:height
for /f "tokens=2 delims==" %%a in ('%wmicheight%') do (
    If %%a LEQ 1 (
        rem skip if height is not bigger than 1
    ) Else (
        rem take the first height value larger than 1
        rem then skip to width
        Set /a "height=%%a"
        goto :width
    )
)
:width
for /f "tokens=2 delims==" %%a in ('%wmicwidth%') do (
    If %%a LEQ 1 (
        rem skip if width is not bigger than 1
    ) Else (
        rem add width found to get total width of all screens
        Set /a "width=width+%%a"
    )
)
echo %width% x %height%

Rob Moore

Posted 2011-04-14T09:51:55.653

Reputation: 349

Please use code blocks. You've been a member here long enough to know how markdown works and how to make it look readable. – Karan – 2015-06-07T07:16:50.610

0

This is my try :

@echo off
Mode 45,3 & color 0A
Title Dislpay Resolution by Hackoo 2018
Set "WMIC_Command=wmic path Win32_VideoController get VideoModeDescription^,CurrentHorizontalResolution^,CurrentVerticalResolution /format:Value"
Set "H=CurrentHorizontalResolution"
Set "V=CurrentVerticalResolution"
Call :GetResolution %H% HorizontalResolution
Call :GetResolution %V% VerticalResolution
echo(
echo     Screen Resolution is : %HorizontalResolution% x %VerticalResolution%
pause>nul & Exit
::****************************************************
:GetResolution 
FOR /F "tokens=2 delims==" %%I IN (
  '%WMIC_Command% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::****************************************************

Hackoo

Posted 2011-04-14T09:51:55.653

Reputation: 589