VBS Script to get a computers screen aspect ratio

The below script will find the screen aspect ratio of a computers primary video controller by querying WMI.

The script stores the horizontal and vertical resolution for each video controller in an array and returns the result of dividing the horizontal by the vertical for the first controller. If the computer has any other controllers there resolutions will also be in the array.

You could modify this script and use it for a few different purposes, I have used this method to deploy wallpapers to computers with the correct aspect ratio.

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

'---------------------------------------
'Get the computers screen Resolution
'---------------------------------------
aspectRatio = GetScreenAspectRatio()

'---------------------------------------
'Perform an action based on the ratio
'---------------------------------------
if aspectRatio = "1.2" or aspectRatio = "1.3" Then
	'Do something
elseif aspectRatio = "1.4" or aspectRatio = "1.5" Then
	'Do something
elseif aspectRatio = "1.6" Then
	'Do something
elseif aspectRatio = "1.7" or aspectRatio = "1.8" or aspectRatio = "1.9" Then
	'Do something
Else
	'Do something else
End If  

'---------------------------------------
'FUNCTION:- GetScreenResolution
'---------------------------------------
Function GetScreenAspectRatio() 
	Dim iHorizontalRes(5)
	Dim iVerticalRes(5)
	
	i = 0
	
	Set colVidControllers = objWMIService.ExecQuery("Select * FROM Win32_VideoController")
			
	For Each objVidController in colVidControllers
		if objVidController.CurrentHorizontalResolution <> "" Then
			iHorizontalRes(i) = objVidController.CurrentHorizontalResolution
			
			iVerticalRes(i) = objVidController.CurrentVerticalResolution
		End If
		
		i = i + 1
	Next

	'Return the resolution of the first screen
	GetScreenAspectRatio = round((iHorizontalRes(0)  / iVerticalRes(0)), 1)
End Function

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.