Thursday, September 25, 2008

Getting Screen Resolution with WMI and PowerShell

Getting the screen resolution with PowerShell is quite simple:

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

foreach ($display in $displays) {
$obj
= New-Object psObject
$obj
Add-Member NoteProperty DeviceName $display.DeviceName
$obj
Add-Member NoteProperty PelsWidth $display.PelsWidth
$obj
Add-Member NoteProperty PelsHeight $display.PelsHeight
$obj
Add-Member NoteProperty BitsPerPel $display.BitsPerPel
$obj
Add-Member NoteProperty DisplayFrequency $display.DisplayFrequency
Write
-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.

Enjoy!

6 comments:

Hal said...

I think ambition is an issue. :)

Unknown said...

Guily as charged..

Malcolm McCaffery said...

Actually it can be done much simpler

$res=[System.Windows.Forms.Screen]::PrimaryScreen.Bounds
"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: http://eztoosoft.com
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.