Database

Windows PowerShell

By Doug Finke, February 27, 2008

A command-line shell and scripting language for Microsoft Windows

Functionality in PowerShell

We've introduced to the power of PowerShell and some examples of how it can be used in daily operations with directories. Now, we'll review some of the common operations within PowerShell and how they work together to achieve time-saving results. This will be fundamental for our deeper dive into PowerShell.

Parameters in PowerShell

PowerShell is about automation and saving time. This is applicable to both using cmdlets -- and as we'll see later-authoring them. In the previous example, three parameters have been specified to three of the cmdlets. -Recurse, -Descending and -AutoSize. PowerShell's parameter binding mechanism accepts -R, -D, and -A as legal representations.

When a cmdlet supports multiple parameters, such as Path and Pass, parameter binding allows the fewest characters to be typed for a match to bind. For example, the characters of "Pat" and "Pas" are sufficient for the "Path" and "Pass" parameters.

Get-Stuff -Pat "c:\" -Pas $true

Additionally, you do not need to be concerned with the order of parameters when authoring cmdlets. This is handled by PowerShell.

Providers in PowerShell

You can access the help facility within PowerShell by typing help about_provider at the command line. The short description on providers is as follows:

Windows PowerShell providers provide access to data and components that would not otherwise be easily accessible at the command line. The data is presented in a consistent format that resembles a file system drive.

Using another cmdlet that ships-Get-PSDrive-yields the following:

[Click image to view at full size]

So what does this mean? It means you can change directory to the registry, certificate store, environment variables, variables you create, functions you create and even your C drive.

Furthermore, because these providers are similar, cmdlets such as dir (aliased for Get-ChildItem) operate as if they were another drive mapped to your system. For example, if you execute cd HKLM: you will navigate to the registry. Typing dir displays the contents of that registry. Similarly, you can cd to SOFTWARE and display its contents.

For information about an alias, you can use the Get-Alias cmdlet. The command Get-Alias dir would retrieve the following:

[Click image to view at full size]

Objects in PowerShell

Since PowerShell is based on .Net, you can create objects using the New-Object cmdlet. The following example generates a random number using the base class libraries; it simulates rolling a single die 1,000 times. Using only 67 characters, a hash table collects and prints the frequency each side is rolled,

[Click image to view at full size]

Looking at each of these commands separately, we can review how they interrelate. First, a range is defined (1-1000). Second, this range is piped into a foreach loop, which creates a hash table with the variable name of $freq. Third, for each value from the hash table, a random number between 1 and 7 is generated. Finally, that value is printed.

cmdlets in PowerShell

A cmdlet is a unit of work in PowerShell. Over 100 cmdlets ship With PowerShell. Want to see just how many cmdlets? Use the system to figure out the system by executing the intuitive Get-Command command.

[Click image to view at full size]

The following two tables outline the unique verbs and nouns with which PowerShell is shipped. The contents can be obtained using the following command:

Get-Command | ForEach { $_.Name.Split("-")[0] } | Sort -Unique

[Click image to view at full size]

[Click image to view at full size]

Syntax in PowerShell

In PowerShell, the syntax is intuitive and familiar. For instance, variables are designated with a dollar symbol ($). Here are some typical methods of creating variables.

[Click image to view at full size]

Since PowerShell is rooted in .Net, if we set $a = "Hello World", $a is of type System.String. From the command line we have access to all the familiar methods and properties of a string; this is demonstrated in the following screenshot.

[Click image to view at full size]

Another out of the box cmdlet is Get-Member. By piping $a to it, the Get-Member cmdlet reflects over the $a (a System.String type) and displays the variable's methods and properties as shown below.

[Click image to view at full size]

Variables in PowerShell

In the following example, $v is a PowerShell variable. The dollar sign designates "v" as a variable; it will store the result of a simple multiplication operation. Note that the data type of $v is undefined. The variable $v is an object and is based on.Net. As such, we can invoke methods such as the GetType() method to display the RunTimeType information. Notice $v is of type Int32 and has a base type of ValueType.

[Click image to view at full size]

Using the same variable $v, we set it to a string; we print out the type information using the GetType() method.

[Click image to view at full size]

Similarly, we can create an array simply by setting the variable to a list of comma-separated values. The GetType() method shows $v is an array of objects. Later we'll cast variables to strongly-typed arrays.

[Click image to view at full size]

Finally, we can key value pairs in a hash table using the @{} PowerShell syntax. This will be verified again through the GetType() method. PowerShell handles both the display and iteration of the namevalue pairs in the hash table.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!