22

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
user281693
  • 615
  • 2
  • 8
  • 20

7 Answers7

56

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}
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • This seems to only find the first monitor. Is there a way to get the resolution for all of the monitors? – Kyle Sep 27 '12 at 12:54
  • 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. – Clayton Aug 02 '16 at 21:05
  • 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%. – KnorxThieus Jun 20 '17 at 18:43
  • 2
    The first method returned empty in `ScreenWidth` and `ScreenHeight`. The second method perfect – Andrei Krasutski Nov 12 '19 at 21:00
  • Hello @ShayLevy, is it possible to get a reference to all those monitors and move some application windows from one to another? – stretavkaBB Nov 08 '21 at 17:00
  • If anyone only want the value of resolution ,Just use `Get-WmiObject -Class Win32_DesktopMonitor | Select -ExpandProperty ScreenHeight` or `[System.Windows.Forms.Screen]::AllScreens | select -ExpandProperty Bounds | select -ExpandProperty Height` – 我零0七 Mar 20 '23 at 02:45
22

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
6

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

wmic path Win32_VideoController get VideoModeDescription

Vlastimil Ovčáčík
  • 2,799
  • 27
  • 29
4

@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

Clayton
  • 225
  • 2
  • 11
2

Here's an answer based on Shays only it formats the results for each screen as per the OPs' example.

PowerShell Code to format the results of: [System.Windows.Forms.Screen]::AllScreens

Add-Type -AssemblyName System.Windows.Forms
$screen_cnt  = [System.Windows.Forms.Screen]::AllScreens.Count
$col_screens = [system.windows.forms.screen]::AllScreens

$info_screens = ($col_screens | ForEach-Object {
if ("$($_.Primary)" -eq "True") {$monitor_type = "Primary Monitor    "} else {$monitor_type = "Secondary Monitor  "}
if ("$($_.Bounds.Width)" -gt "$($_.Bounds.Height)") {$monitor_orientation = "Landscape"} else {$monitor_orientation = "Portrait"}
$monitor_type + "(Bounds)                          " + "$($_.Bounds)"
$monitor_type + "(Primary)                         " + "$($_.Primary)"
$monitor_type + "(Device Name)                     " + "$($_.DeviceName)"
$monitor_type + "(Bounds Width x Bounds Height)    " + "$($_.Bounds.Width) x $($_.Bounds.Height) ($monitor_orientation)"
$monitor_type + "(Bits Per Pixel)                  " + "$($_.BitsPerPixel)"
$monitor_type + "(Working Area)                    " + "$($_.WorkingArea)"
}
)

Write-Host "TOTAL SCREEN COUNT: $screen_cnt"
$info_screens

Output for the secondary monitor in landscape mode. 1920 x 1200

# TOTAL SCREEN COUNT: 2
# Primary Monitor    (Bounds)                          {X=0,Y=0,Width=2560,Height=1600}
# Primary Monitor    (Primary)                         True
# Primary Monitor    (Device Name)                     \\.\DISPLAY1
# Primary Monitor    (Bounds Width x Bounds Height)    2560 x 1600 (Landscape)
# Primary Monitor    (Bits Per Pixel)                  32
# Primary Monitor    (Working Area)                    {X=0,Y=0,Width=2560,Height=1560}
# Secondary Monitor  (Bounds)                          {X=2560,Y=0,Width=1920,Height=1200}
# Secondary Monitor  (Primary)                         False
# Secondary Monitor  (Device Name)                     \\.\DISPLAY2
# Secondary Monitor  (Bounds Width x Bounds Height)    1920 x 1200 (Landscape)
# Secondary Monitor  (Bits Per Pixel)                  32
# Secondary Monitor  (Working Area)                    {X=2560,Y=0,Width=1920,Height=1160}

Output for the secondary monitor in portrait mode. 1200 x 1920

# TOTAL SCREEN COUNT: 2
# Primary Monitor    (Bounds)                          {X=0,Y=0,Width=2560,Height=1600}
# Primary Monitor    (Primary)                         True
# Primary Monitor    (Device Name)                     \\.\DISPLAY1
# Primary Monitor    (Bounds Width x Bounds Height)    2560 x 1600 (Landscape)
# Primary Monitor    (Bits Per Pixel)                  32
# Primary Monitor    (Working Area)                    {X=0,Y=0,Width=2560,Height=1560}
# Secondary Monitor  (Bounds)                          {X=2560,Y=0,Width=1200,Height=1920}
# Secondary Monitor  (Primary)                         False
# Secondary Monitor  (Device Name)                     \\.\DISPLAY2
# Secondary Monitor  (Bounds Width x Bounds Height)    1200 x 1920 (Portrait)
# Secondary Monitor  (Bits Per Pixel)                  32
# Secondary Monitor  (Working Area)                    {X=2560,Y=0,Width=1200,Height=1880}
Ste
  • 1,729
  • 1
  • 17
  • 27
0

You can get all available resolution with this command:

$Query = "SELECT * FROM CIM_VideoControllerResolution"
$res = Get-WMIObject -query $Query | Select Caption
S0me0ne
  • 403
  • 2
  • 11
0

For Short, this gets you the first screen (if you have many) width and height separately

$height = (((Get-WmiObject -Class Win32_VideoController).VideoModeDescription  -split '\n')[0]  -split ' ')[2]
$width = (((Get-WmiObject -Class Win32_VideoController).VideoModeDescription  -split '\n')[0]  -split ' ')[0]
mohaa8844
  • 419
  • 6
  • 10