Computing in the real world
SEARCH FOR: IN:
Guest  Level 00    Register Log in

Real World Computing

Shell power for Windows

29th June 2006 [PC Pro]

PSH [C:\]: [string] $s = "foobar"

PSH [C:\]: [int] $i=21

PSH [C:\]: $I+$S

Cannot convert value "foobar" to type "System.Int32". Error: "Input string was not in a correct format."

At line:1 char:4

+ $I+$ <<<< S

Here, PowerShell creates a string variable $s and an integer variable $i, and then quite correctly raises an error when you attempt to add them together. PowerShell enhances type safety through a synthetic type system: by using a series of XML files that provide the type definitions, it extends and normalises the underlying .NET objects. For example, in the PROCESS class, the name of the process is held by the PROCESSNAME property, while for services the service name is held in the SERVICENAME property. However, Enhanced PowerShell adds a generic NAME parameter to both classes, thus simplifying the writing of polymorphic scripts - another example of knowledge leverage.

By default, PowerShell will automatically pipe any output to the OUT-DEFAULT cmdlet for formatting, but you're able to improve and extend the default formatting, as all the formatting information, which you can modify or extend, is stored in XML form in the PowerShell install folder. You don't have to just use PowerShell's defaults, although they're often good enough for most simple applications. You can use the FORMAT-LIST or FORMAT-TABLE cmdlets to be clearer about which properties you want to look at, and if you want fixed-width output you can apply .NET formatting strings to get the precise layout you want.

For more information about PowerShell, read the PowerShell Team blog or visit my PowerShell subsite (www.reskit.net), which has information and a bunch of sample scripts to look at.

Previous page 1 | 2 | 3 | 4 | 5