Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Screen resolution using WMI/powershell in Windows 7

Tags:

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
like image 305
user281693 Avatar asked Nov 01 '11 14:11

user281693


People also ask

How do I get screen resolution with WMI?

The Get-DisplayResolution WMI cmdlet shows the display resolution for Windows 10 computers. Use the following PowerShell script to get the current screen resolution. Provide the computer name as the parameter and run the script. The resolution of the screen will be displayed.

How do I find my screen resolution in PowerShell?

Description. The Get-DisplayResolution cmdlet shows the display resolution for Windows Server® 2012 in Server Core mode. For a width of 1920 pixels and a height of 1080 pixels, the cmdlet displays 1920x1080. You can use the Set-DisplayResolution cmdlet to change the resolution.

How do I find my screen resolution Windows 7?

Right-click on the desktop of your computer and select "Screen resolution". Click the drop-down menu labeled "Resolution" and use the slider to select the desired screen resolution. Click "Apply".


1 Answers

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}
like image 119
Shay Levy Avatar answered Nov 15 '22 03:11

Shay Levy