Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Windows PowerShell


Doug Finke works for Lab49, a company that builds advanced applications for the financial service industry. Over the last 20 years, Doug has been a developer and author working with numerous technologies. You can catch up with Doug at his blog.

Microsoft PowerShell is an operating environment for commands -- cmdlets, functions, filters, scripts, aliases, and executables. PowerShell is based on the .NET Framework, which means that out of the box your existing investment in .NET components is preserved. An additional benefit is PowerShell's extensibility. PowerShell "Snap-ins" let you seamlessly integrate existing .NET components. By eliminating the IDE, PowerShell offers new feedback loops by removing the compile, link, and go steps. Additionally, PowerShell provides new ways to work with cumbersome, long-standing abstractions. For example, instead of launching to a specific area of the registry using regedt32, PowerShell lets you use the dir cmdlet using wildcards on the registry.

[Click image to view at full size]

To provide rapid development of software solutions, PowerShell pipes objects and provides parameter binding. By reducing the number of lines of code, you realize an enormous benefit during testing and re-factoring for change.

Why Learn PowerShell?

For administrators, PowerShell provides efficient, powerful access to key indicators of the operating system. (It is an abstraction much the way Visual Basic was to the Win32 APIs.) For developers, PowerShell is helpful in SQL scripting or developing DLLs. The concepts and cmdlets of PowerShell offer new ways to work with old information.

Developers need to continually find ways to quickly, effectively, and reliably deliver software solutions. A key element in this process is exchanging higher-level concepts for low-level ones. Consider the following example that demonstrates several concepts.

[Click image to view at full size]

First, this PowerShell example illustrates the use of the range operation. Second, although no explicit looping or print commands are defined, note that PowerShell iterated and printed the numbers in the range. This is not to say that PowerShell lacks looping constructs (it has foreach, for, do, and while). Rather, it is an example of abstracting away unnecessary details. This is a key idea behind PowerShell.

Here is an example of using ForEach and a glimpse at "piping" with PowerShell.

[Click image to view at full size]

Note the $_ variable in the previous example. This is an automatic PowerShell variable indicating the current object in the pipeline (variables will be discussed later). In short, PowerShell preserves the looping constructs you are familiar with and provides simpler ways to get things done.

How Does PowerShell Work?

When starting to work with PowerShell, one of the first things you'll use is a cmdlet (pronounced "Command Let"). A cmdlet is a single-feature, built-in command -- a building block by which an operation is completed. For example, one such cmdlet is Get-Date. Note the verb ("Get") and noun ("Date)" are separated by a dash; all cmdlets use this memorable verb-noun form. You can imagine what Get-Date does -- it returns the date. PowerShell also makes use of familiar commands, such as dir. (Digging a bit deeper, you find that dir is a memorable alias for the Get-ChildItem cmdlet.) The cmdlets delivered with PowerShell are covered later.

The runtime performs another role -- information used within the environment is done through structured information: objects with properties. These data objects are compatible with the .NET Framework. Data objects are supplied to and from commands through the pipeline. PowerShell pipes objects and provides parameter binding. This innovation provides rapid development of software solutions for many challenges. Developers are not burdened with creating "plumbing" between cmdlets.

How Does PowerShell compare with .NET?

PowerShell provides features that are accessible and robust. For example, you can create an XML document in a variable based on the contents of an XML file. Refer to the following example:

[Click image to view at full size]

The task can be completed in a few keystrokes. In contrast, to accomplish the same task in .NET requires several steps and background information: launch the IDE, create a project, know the System.Xml namespace, and use the Load method. Yet even after all those steps, the data has yet to be printed. To print the data requires returning to the IDE, adding more code, compiling, running, and waiting for the results. In PowerShell, you can easily; see the nodes:

[Click image to view at full size]

Programs help users concentrate and focus by quickly providing user feedback to issued commands. (Inellisense is a good example of employing such "feedback loops.") PowerShell, is a terrific high-speed feedback loop at many levels.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

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.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.