Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell script returning the wrong screen resolution

I just wrote a simple PowerShell script to get the screen resolution of my monitor, but it seems to be returning the wrong values.

# Returns an screen width and screen height of maximum screen resolution
function Get-ScreenSize {
    $screen = [System.Windows.Forms.Screen]::PrimaryScreen
    $width  = $screen.Bounds.Width
    $height = $screen.Bounds.Height
    return $width, $height
}

Get-ScreenSize

I am running this script on a 4k monitor with the resolution set at 3840 x 2160, but it is giving me the following output:

1536

864

Is there anything that would cause System.Windows.Forms.Screen to get the wrong "Bounds" values?

like image 400
tjwrona1992 Avatar asked Sep 20 '25 14:09

tjwrona1992


2 Answers

Well I didn't exactly find out why I was getting such strange results... but I did find another approach that actually seems simpler and appears to be accurate.

$vc = Get-WmiObject -class "Win32_VideoController"
$vc.CurrentHorizontalResolution
$vc.CurrentVerticalResolution

This will print the current screen resolution and appears to be giving me accurate results which is what I was actually looking for. If anyone figures out what could cause the other approach to produce inaccurate results I would still really like to know why it is happening though...

like image 81
tjwrona1992 Avatar answered Sep 22 '25 07:09

tjwrona1992


It's because that command gives you the scaled resolution. If you're running 3840 x 2160 but you're not running on 100% scaling you'll get a different value.

like image 22
Julian Chow Avatar answered Sep 22 '25 06:09

Julian Chow