Thursday, September 25, 2008

Getting Screen Resolution with WMI and PowerShell

Getting the screen resolution with PowerShell is quite simple:

param( [string]$strComputer = "." )
= Get-WmiObject `
-class "Win32_DisplayConfiguration" `
-computername $strComputer

foreach ($display in $displays) {
= New-Object psObject
Add-Member NoteProperty DeviceName $display.DeviceName
Add-Member NoteProperty PelsWidth $display.PelsWidth
Add-Member NoteProperty PelsHeight $display.PelsHeight
Add-Member NoteProperty BitsPerPel $display.BitsPerPel
Add-Member NoteProperty DisplayFrequency $display.DisplayFrequency
-Output $obj

Setting the resolution is not so simple. There are a few ways to do this, none of them completely native to PowerShell. You can pass parameters to a compiled executable like this one listed on CodeProject or Qres (Thanks Hal for the links). If warranted, and ambition wasn't an issue, you could write inline C#. Check out Lee Holmes' Invoke-Inline for a wrapper for this.



halr9000 said...

I think ambition is an issue. :)

Wes Stahler said...

Guily as charged..

Malcolm McCaffery said...

Actually it can be done much simpler

"Screen width: " + $res.Width + " Screen Height: " + $res.Height

Anonymous said...

Thanks for your information, i have read it, very good!
With DVD Ripper you can transfer the entire DVD film into all popular video or audio formats.
More info you can visit:
DVD Ripper , DVD Ripper for Mac , DVD To iPod , DVD To iPhone , DVD To AVI for Mac , DVD To Apple TV for Mac

Anonymous said...

Rip Blu-ray for Mac with Blu-ray Ripper for Mac, snow Leopard included.- Free Download and have a try.
Ultimate Blu-ray video converter is actually an 3-in-one software including Blu-ray Ripper, DVD Ripper and Video Converter.
Blu-ray ripper enables you directly rip and convert your Blu-ray movies and general DVDs, even the protected DVDs and Blu-ray DVDs into other popular audio and video formats.

Pakistan distribution companies said...

For a moment while I was account your commodity I addled to me that I consistently anticipation about this thing. But could not put it into words! How you do this is marvelous.