25 November 2007

Beginning PowerShell

Introduction

Microsoft's PowerShell is a scripting environment based on the .Net framework. PowerShell is aimed at replacing Windows Cmd and Windows Scripting Host (WSH) for administrative tasks. What interests me is that I can use .Net objects interactively and in a script, without having to write and compile a program. As I explore PowerShell, I'll write about features that I find interesting and useful.

In this first article, I describe really basic use of the PowerShell console. I assume you downloaded and installed Powershell.

Help!

Powershell installation provides a lot of on-line help. You obtain information using the get-help cmdlet (essentially a built-in command). There's so much information that the trick is to figure out the appropriate help topic:

> help # or 'get-help'
< 299 lines … >
> help about_Arithmetic_Operators
TOPIC
    Arithmetic operators

SHORT DESCRIPTION
    Operators that can be used in the Windows PowerShell to perform
    mathematical operations
…

You can also download the on-line help as a CHM file (search for "Windows PowerShell Graphical Help File").

Interactive Mode

You can enter commands for processing immediately:

> 'abc' + 'def' # String concatenation
abcdef
> 5+6 # Addition
11
> 5*6 # Multiplication
30
> 30%7 # Remainder
2

The '#' marks the start of a comment. All text from '#' to the end of the line is a comment.

Using .Net Classes and Objects

How long is a string (said with a straight face)? Since we are using .Net String objects, each string should have a Length property:

> 'abc'.Length
3
> 'def'.Length
3
> ('abc' + 'def').Length
6

You can find the methods of an object using get-member.

> 'abc' | get-member


   TypeName: System.String

Name             MemberType            Definition
----             ----------            ----------
…
get_Length       Method                System.Int32 get_Length()
…

What's 2 to the power of 8? The .Net System.Math class defines a set of common mathematical functions. Again, you can use the get-member cmdlet to find out what functions are available in any class, but this time, with the -static option because the class, not individual objects, define these functions.

> [math] | get-member -static


   TypeName: System.Math

Name            MemberType Definition
----            ---------- ----------
…
Pow             Method     static System.Double Pow(Double x, Double y)
…
> [math]::Pow(2,8)
256

The System.Math is pre-loaded by PowerShell and can be accessed as [System.Math], [Math] or [math]. Square brackets refer to .Net classes and some aliases have been defined for these classes.

How do you know whether an object or a class has the required function? No easy answer: trial-and-error, and experience with other class libraries.

Tab Expansion and Aliases

It's pretty tedious typing a full cmdlet string, so there are two shortcuts available: tab expansion and aliases.

If you enter get-m<TAB>, you should see Get-Member. For some reason, you have to enter the name of cmdlet up to the '-' (dash) character before tab expansion works.

The get-member cmdlet also aliased as gm. What aliases are available? Use the get-alias cmdlet, which is also aliased as gal, for a list of available aliases.

> Get-Alias

CommandType     Name                                                Definition
-----------     ----                                                ----------
Alias           gal                                                 Get-Alias
…
Alias           gm                                                  Get-Member
…

Conclusion

At this point, you can run some simple commands using PowerShell and know how to find some on-line information.