在Windows 7中使用WMI / powershell获取屏幕分辨率

时间:2011-11-01 14:08:31

标签: windows-7 powershell wmi

我使用以下脚本在Windows中使用WMI获取屏幕分辨率。当计算机处于横向模式时,脚本可以正常工作,但在纵向模式下,脚本返回不正确的值。在XP中正常工作,并没有在Vista中尝试。任何人都可以确认这是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

7 个答案:

答案 0 :(得分:30)

对于记录,PowerShell代码为:

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

我在横向或纵向模式下获得相同的值。

<强>更新

在多监视器环境中,您可以使用以下命令获取所有监视器的信息:

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}

答案 1 :(得分:9)

您可以从Win32_VideoController WMI类中获取此信息。 VideoModeDescription属性包括屏幕分辨率和颜色深度。

(Get-WmiObject -Class Win32_VideoController).VideoModeDescription;

结果

1600 x 900 x 4294967296 colors

答案 2 :(得分:4)

与其他答案相同,但对于普通cmd:

wmic path Win32_VideoController get VideoModeDescription

答案 3 :(得分:3)

@Shay Levy上面的回答准确地报告了PowerShell会话启动时活动的宽度/高度。如果在PS启动后旋转监视器,它将继续报告原始的,现在不正确的值。

SystemInformation 类提供了另一种获取方向的方法,即使在会话启动后旋转显示,它也会在当前PS会话中发生变化。

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

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

旋转显示器,然后......

[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

答案 4 :(得分:0)

这是我的尝试:

@echo off
Mode 45,3 & color 0A
Title Dislpay Resolution by Hackoo 2018
Set "WMIC_Command=wmic path Win32_VideoController get VideoModeDescription^,CurrentHorizontalResolution^,CurrentVerticalResolution /format:Value"
Set "H=CurrentHorizontalResolution"
Set "V=CurrentVerticalResolution"
Call :GetResolution %H% HorizontalResolution
Call :GetResolution %V% VerticalResolution
echo(
echo     Screen Resolution is : %HorizontalResolution% x %VerticalResolution%
pause>nul & Exit
::****************************************************
:GetResolution 
FOR /F "tokens=2 delims==" %%I IN (
  '%WMIC_Command% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::****************************************************

答案 5 :(得分:0)

这是一个基于Shays的答案,只是按照OP的示例格式化每个屏幕的结果。

用于格式化以下结果的PowerShell代码:[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

横向模式下辅助显示器的输出。 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}

在纵向模式下辅助显示器的输出。 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}

答案 6 :(得分:-1)

您可以使用此命令获得所有可用的分辨率:

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