3

I'm trying get the current screen resolution on windows via command line.
Based on most of the answers I've found, I should use:

wmic desktopmonitor get screenheight, screenweight

But this returns the max supported resolution of the display device, not the current one, which is what I need.

Example:
I'm using a 4k monitor, but currently set to display only at 1920x1080. When I run the command above, I get:

ScreenHeight  ScreenWidth
2160          3840

enter image description here

How do I get the current screen resolution on windows via command line?

  • Interesting, Why would you need this exactly? – Sickest May 15 '19 at 1:39
  • I'm developing a multi OS app in python that needs to get current screen resolution via command line. I'm trying to avoid 3rd party libraries and stick with the default OS binaries or python builtin functions only. – CONvid19 May 15 '19 at 1:46
  • 1
    This question has already been asked at StackOverflow and Server Fault. You may need to use PowerShell. See stackoverflow.com/questions/7967699/… and serverfault.com/questions/825360/… – DrMoishe Pippik May 15 '19 at 2:44
  • 1
    It's not duplicate, but actually a good example of what's not working. – CONvid19 May 15 '19 at 3:06
2

Dealing with high DPI made this somewhat challenging because most Windows API functions return a scaled version of the resolution for compatibility unless the application declares high DPI awareness. Inspired by this Stack Overflow answer I wrote this PowerShell script:

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class PInvoke {
    [DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hwnd);
    [DllImport("gdi32.dll")] public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
}
"@
$hdc = [PInvoke]::GetDC([IntPtr]::Zero)
[PInvoke]::GetDeviceCaps($hdc, 118) # width
[PInvoke]::GetDeviceCaps($hdc, 117) # height

It outputs two lines: first the horizontal resolution, then the vertical resolution.

To run it, save it to a file (e.g. screenres.ps1) and launch it with PowerShell:

powershell -ExecutionPolicy Bypass .\screenres.ps1
| improve this answer | |
  • Your solution works perfectly, thank you. – CONvid19 May 16 '19 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.