Search code examples
windowsbatch-filecmdresolution

Get screen resolution as a variable in cmd


I need a script to copy a specific image depending on the screen resolution being used. so far I've found that wmic desktopmonitor get screenheight is giving me the appropriate output but i'm having trouble parsing it to a useable variable the problem is that the output is on three lines and I only need the information from the second one.

Can anyone help?


Solution

  • You can even get more parameters with one single wmic:

    for /f %%i in ('wmic desktopmonitor get screenheight^,screenwidth /value ^| find "="') do set "%%f"
    echo your screen is %screenwidth% * %screenheight% pixels
    

    If you need to have your own variablenames, it's a bit more complicated:

    for /f "tokens=1,2 delims==" %%i in ('wmic desktopmonitor get screenheight^,screenwidth /value ^| find "="') do (
      if "%%i"=="ScreenHeight" set height=%%j
      if "%%i"=="ScreenWidth" set width=%%j
    )
    echo your screen is %width% * %height% pixels
    

    if you need only one value:

    for /f "tokens=2 delims==" %%i in ('wmic desktopmonitor get screenheight /value ^| find "="') do set height=%%i
    echo %height%