Wednesday, June 20, 2007

Playing around with Powershell

For those living under a rock the newish command shell from Microsoft called Powershell (aka 'Monad') is quite nice.

Get Powershell

It treats everything as object that you can pipe around and you play around with file system, registry, wmi, .net objects, com objects, custom stuff all in the same way.

Let's say you want to play around with WMI and look at your computer.

PS C:\Users\Simon> Get-WmiObject Win32_ComputerSystem

Domain              : WORKGROUP
Manufacturer        : System manufacturer
Model               : System name
Name                : REBORN
PrimaryOwnerName    : Simon
TotalPhysicalMemory : 1609498624

or let's say that you want to find the description of the 3 processes that currently uses the most CPU:

PS C:\Users\Simon> Get-Process |
>> sort-object -property CPU -descending |
>> select-object -first 3 -property Description

Windows Media Player
Desktop Window Manager

Or if you for some reason feel the need to show a messagebox (and yes, the messagebox did show and I hit yes):

PS C:\> [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
PS C:\> [Windows.Forms.MessageBox]::Show("Hello World")

No comments: