Monthly Archive


Query me no more

One of the things I noticed in the recent Scripting Games was that a lot of the scripts would do things like this

$query = "Select MaxClockSpeed from Win32_Processor"
$proc = Get-WmiObject -Query $query
Write-Host "Speed: " $proc.MaxClockSpeed

Create a query to access the Win32_Processor class to get the MaxClockSpeed.  Run the query and then use Write-Host to format and output the results.  We’ll come back to use Write-Host like this another time.

This construction is based on the way things used to be done with VBScript.  With PowerShell we have much easier ways to get to the same result.

Get-WmiObject -Class Win32_Processor | Format-List MaxClockSpeed

will produce the same answer. if you really need the formatting

Get-WmiObject -Class Win32_Processor | Format-List @{Name='Speed'; Expression={$_.MaxClockSpeed}}

or even

Write-Host "Speed: " (Get-WmiObject -Class Win32_Processor).MaxClockSpeed

Get-WmiObject does a great job of returning the information from a WMI class. Use its power and simplicity to make your scripts quicker to write and easier to understand

Leave a Reply