By user281693


2011-11-01 14:08:30 8 Comments

I am using the following script to get screen resolution in Windows using WMI. The script works fine when the computer is in landscape mode but returns incorrect values when in portrait mode. Works properly in XP and did not try in Vista. Can anyone confirm this is bug in Windows 7 WMI.

strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_DesktopMonitor",,48) 
For Each objItem in colItems 
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "Win32_DesktopMonitor instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "ScreenHeight: " & objItem.ScreenHeight
    Wscript.Echo "ScreenWidth: " & objItem.ScreenWidth
Next

5 comments

@Clayton 2016-08-02 21:09:11

@Shay Levy's answer above accurately reports the Width/Height that was active when the powershell session was launched. If you rotate monitor after PS launch, it continues to report the original, now incorrect values.

The SystemInformation class provides another way to get orientation, and it changes in the current PS session even if the display is rotated after the session launch.

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SystemInformation]::ScreenOrientation
Angle0

[System.Windows.Forms.SystemInformation]::PrimaryMonitorSize
IsEmpty                            Width                           Height
-------                            -----                           ------
False                              1680                             1050

Rotate monitor, then...

[System.Windows.Forms.SystemInformation]::ScreenOrientation
Angle90

[System.Windows.Forms.SystemInformation]::PrimaryMonitorSize
IsEmpty                            Width                           Height
-------                            -----                           ------
False                              1050                             1680

https://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation(v=vs.110).aspx

@Vlastimil Ovčáčík 2014-05-14 19:12:19

Same as the other answers, however for the plain cmd:

wmic path Win32_VideoController get VideoModeDescription

@Trevor Sullivan 2014-03-31 21:23:34

You can grab this from the Win32_VideoController WMI class. The VideoModeDescription property includes the screen resolution and the color depth.

(Get-WmiObject -Class Win32_VideoController).VideoModeDescription;

Result

1600 x 900 x 4294967296 colors

@Shay Levy 2011-11-01 14:34:01

For the record, the PowerShell code is:

Get-WmiObject -Class Win32_DesktopMonitor | Select-Object ScreenWidth,ScreenHeight

I get the same values in Landscape or in Portrait mode.

UPDATE:

In a multi monitor environment you can get the info for all monitors with:

PS> Add-Type -AssemblyName System.Windows.Forms
PS> [System.Windows.Forms.Screen]::AllScreens


BitsPerPixel : 32
Bounds       : {X=0,Y=0,Width=1280,Height=800}
DeviceName   : \\.\DISPLAY1
Primary      : True
WorkingArea  : {X=0,Y=0,Width=1280,Height=770}

BitsPerPixel : 32
Bounds       : {X=1280,Y=0,Width=1920,Height=1200}
DeviceName   : \\.\DISPLAY2
Primary      : False
WorkingArea  : {X=1280,Y=0,Width=1920,Height=1170}

@Kyle 2012-09-27 12:54:14

This seems to only find the first monitor. Is there a way to get the resolution for all of the monitors?

@Shay Levy 2012-09-27 17:49:00

i just update the thread, give it a try

@Clayton 2016-08-02 21:05:16

The second method here accurately reports the Width Height that was active when the powershell session was launched. If you rotate monitor after PS launch, it continues to report the original, now incorrect values. See answer below for another method that works in the same PS session even after monitor is rotated.

@KnorxThieus 2017-06-20 18:43:03

Very nice solution, regarding to the update (WMI solution does not work for me, Win 10). Unfortunately, you have to multiply width and height of the bound objects by dpi scaling factor (to configure in Windows Settings > System > Display), e. g. by 1.25 for 125%.

@S0me0ne 2012-05-09 16:32:58

You can get all available resolution with this command:

$Query = "SELECT * FROM CIM_VideoControllerResolution"
$res = Get-WMIObject -query $Query | Select Caption

Related Questions

Sponsored Content

15 Answered Questions

[SOLVED] Setting Windows PowerShell environment variables

  • 2009-04-03 17:19:35
  • Vasil
  • 555900 View
  • 551 Score
  • 15 Answer
  • Tags:   windows powershell

19 Answered Questions

[SOLVED] Determine installed PowerShell version

  • 2009-12-01 11:30:03
  • MagicAndi
  • 2840883 View
  • 2531 Score
  • 19 Answer
  • Tags:   powershell

22 Answered Questions

[SOLVED] PowerShell says "execution of scripts is disabled on this system."

7 Answered Questions

[SOLVED] How do you comment out code in PowerShell?

2 Answered Questions

[SOLVED] Using variables in vbs WMI methods

3 Answered Questions

[SOLVED] Use WMI to get current Windows username in VBA

0 Answered Questions

WMI 0x80041013 "Provider load failure"

1 Answered Questions

WMI Get Object Access Denied

0 Answered Questions

Can't get IIS WMI information in ASP.Net web application

  • 2011-12-26 09:29:48
  • Mr.Wang from Next Door
  • 1369 View
  • 2 Score
  • 0 Answer
  • Tags:   asp.net iis wmi

1 Answered Questions

[SOLVED] Unable to obtain physical memory informatin using powershell

Sponsored Content