PowerShell for Windows Admins

May 24 2010   3:14PM GMT

Query me no more

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: