Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

Get Screen resolution using WMI/powershell in Windows 7

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 & "
ootCIMV2") 
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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...