4

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?

5
  • What values do you get when using the WorkingArea instead of the Bounds property? Commented Mar 12, 2016 at 21:58
  • I get 3840 x 2060 which is very close, but still not quite correct. Commented Mar 12, 2016 at 21:59
  • That looks correct. Full width, and height minus the taskbar. Commented Mar 12, 2016 at 22:00
  • Okay, hmm then I wonder why bounds is returning the wrong value... Commented Mar 12, 2016 at 22:01
  • Looking at the reference implementation for Screen, it looks like Bounds uses the virtual screen size (not entirely sure, why the numbers are smaller than expected, though), while WorkingArea uses the multi-monitor API. I don't know how to fix the PowerShell commandlet, though. Commented Mar 12, 2016 at 22:14

3 Answers 3

6

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...

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

That is very interesting, but I guess it makes sense if that's the command apps use internally to scale themselves properly.
-2

That's odd.

Why on earth has Microsoft only provided the Get-DisplayResolution cmdlet with Server Core?

That edition ships without a Start-button... and according to the comment above on the returned display size (minus start-bar); I won't be surprised to hear that cmdlet is using the same .NET code library.

Quick search in my HKLM\SYSTEM\CurrentControlSet\Control lists a few keys for monitors and values per screen, but nothing useful.


Edit: see Q7967699.

PS D:\Scripts> Add-Type -AssemblyName System.Windows.Forms

PS D:\Scripts> [System.Windows.Forms.Screen]::AllScreens


BitsPerPixel : 32
Bounds       : {X=0,Y=0,Width=3840,Height=2160}
DeviceName   : \\.\DISPLAY1
Primary      : True
WorkingArea  : {X=0,Y=0,Width=3840,Height=2120}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.