This example will list the preferred resolution for all the connected monitors.
The Code:
On Error Resume Next
strComputer = "."
strQuery = "SELECT PreferredMonitorSourceModeIndex, MonitorSourceModes " & _
"FROM WmiMonitorListedSupportedSourceModes"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\ROOT\WMI")
Set colItems = objWMIService.ExecQuery(strQuery, , 48)
For Each objItem In colItems
i = objItem.PreferredMonitorSourceModeIndex
wscript.stdout.writeline "InstanceName: " & objItem.instancename
wscript.stdout.writeline "Horizontal: " & objItem.MonitorSourceModes(i).HorizontalActivePixels
wscript.stdout.writeline "Vertical: " & objItem.MonitorSourceModes(i).VerticalActivePixels
Next
We first get the WMI Service. It is not creatable.
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\ROOT\WMI")
Next, set up our query using WQL. WQL is very similar to SQL.
strQuery = "SELECT PreferredMonitorSourceModeIndex, MonitorSourceModes " & _
"FROM WmiMonitorListedSupportedSourceModes"
The WMI class WmiMonitorListedSupportedSourceModes has 5 properties: InstanceName, Active, MonitorSourceModes, NumOfMonitorSourceModes, and PreferredMonitorSourceModeIndex. MonitorSourceModes is an array, and we must query PreferredMonitorSourceModeIndex to determine which element of the array contains the information we seek.
Now let's execute our query
Set colItems = objWMIService.ExecQuery(strQuery, , 48)
and loop through the results:
For Each objItem In colItems
i = objItem.PreferredMonitorSourceModeIndex
wscript.stdout.writeline "InstanceName: " & objItem.instancename
wscript.stdout.writeline "Horizontal: " & objItem.MonitorSourceModes(i).HorizontalActivePixels
wscript.stdout.writeline "Vertical: " & objItem.MonitorSourceModes(i).VerticalActivePixels
Next