July 21st, 2005

How Can I Determine the Current Screen Resolution Being Used on a Computer?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine the current screen resolution being used on a computer? You know, things like 1600×1200 resolution, 16-bit color, that kind of information.

— FE

SpacerHey, Scripting Guy! AnswerScript Center

Hey, FE. Remember when you were a kid and your mom would have that annoying habit of beginning a discussion by answering the question you didn’t ask? For example, suppose you said, “Mom, can I go over to Billy’s?” Inevitably she would answer, “No, you cannot go down to the lake without an adult present. But you can go over to Billy’s as long as you two stay away from the lake.”

Remember how much fun that was? Well, in case you’ve forgotten, we’ll refresh your memory: “No, you cannot use a script to change the screen resolution on a computer. You can use a script to retrieve information about the current screen resolution, but that information is read-only.”

Oh, and stay away from the lake; after all, you have work to do!

We know you didn’t ask about changing screen resolution using a script, but we felt we’d better mention that lest we get millions of emails asking that very question. (We’ll probably still get millions of emails asking that very question, but at least we tried.) For better or worse (and yes, we agree that this qualifies as “for worse”) there’s no built-in way to change the screen resolution using a script. Sorry.

But if all you want to do is retrieve the current resolution settings the news is a bit better:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colItems = objWMIService.ExecQuery _ (“Select * From Win32_DisplayConfiguration”)

For Each objItem in colItems Wscript.Echo “Name: ” & objItem.DeviceName Wscript.Echo “Color depth: ” & objItem.BitsPerPel Wscript.Echo “Horizontal resolution: ” & objItem.PelsWidth Wscript.Echo “Vertical resolution: ” & objItem.PelsHeight Wscript.Echo Next

Of course it’s a simple little script; the Scripting Guys are simple little guys! All we do is connect to the WMI service and then use this query to retrieve all the instances of the Win32_DisplayConfiguration class:

Set colItems = objWMIService.ExecQuery _
    (“Select * From Win32_DisplayConfiguration”)

We then echo back the values of various properties, including DeviceName, BitsPerPel (the color depth), PelsWidth (the horizontal resolution), and PelsHeight (the vertical resolution). In other words, property values similar to this:

Name: RADEON IGP 345M
Color depth: 32
Horizontal resolution: 1024
Vertical resolution: 768

Scripting Guys Trivia. The term pels is short for picture elements, more commonly referred to as pixels. Hey, when you’re a Scripting Guy you’re supposed to know stuff like that!

So there you have it: a way to retrieve the current screen resolution on a computer. We’re glad we could help, but this does not mean you can go anywhere near that lake! (Yes, we are mean, aren’t we? But you’ll thank us for that someday.)

Author

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • G L

    FYI the code snippet on this page contains so-called “smart” quotes and you will need to search & replace these before trying-out the code in a real test script (in a plain text environment). Terminal fixed monospace plain fonts only please, thank you!