How to get the current screen resolution on windows via command line?
Solution 1:
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
Solution 2:
I had a similar problem.
Try this:
wmic PATH Win32_VideoController GET CurrentVerticalResolution,CurrentHorizontalResolution