How to Get the Current Screen Resolution on Windows via Command Line?
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...
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
How do I get the current screen resolution on windows via command line?
2 Answers
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
I had a similar problem.
Try this:
wmic PATH Win32_VideoController GET CurrentVerticalResolution,CurrentHorizontalResolution