PowerShell: Introduction

PowerShell is a new command shell from Microsoft, based on the Win32 console. It is a command prompt and scripting environment and it can even be both at the same time. To be more precise, Windows PowerShell is Microsoft’s task automation framework, consisting of a command-line shell and associated scripting language built on top of, and integrated with the .NET Framework. PowerShell provides full access to COM and WMI, enabling administrators to perform administrative tasks on both local and remote Windows systems. Active Directory domains are becoming larger and more complex and administrators still chained to GUI tools and the Microsoft Management Console are slowly becoming history.

PowerShell Concepts

PowerShell is object-based, not text-based, and the output of a PowerShell command (the object) can be piped into another command without additional programming. With traditional scripting, if you wanted to use the output of one command in another, additional programming would be required to manipulate the data in a format the second command could understand.

PowerShell Commands are customisable and are referred to as cmdlets (pronounced command-lets). With the installation of PowerShell there are over a hundred cmdlets for you to get up close and personal with. The PowerShell team created aliases to use the traditional commands we have become accustomed to (DOS: dir, cd, del, copy, etc … and UNIX: ls, man, etc…). PowerShell allows you to create your own aliases as well as creating your own cmdlets.

PowerShell is a Command line interpreter and a scripting environment. In a nutshell you have the best of both worlds within PowerShell. DOS was a command line interpreter: enter command, get output. You could use batch files, but in reality a batch file just entered the commands for you. VBScript utilises WSH (Windows Scripting Host), but you can’t enter VBScript code in a command prompt. With PowerShell not only can you enter commands, you can build script-blocks from the PowerShell command line.

If a command is an executable program, PowerShell.exe launches it in a separate process; if it is a cmdlet, it is executed in the PowerShell process. PowerShell also provides an interactive command line interface, wherein the commands can be entered and their output displayed. It offers customisable tab completion but lacks syntax highlighting.

PowerShell pipelines are used to compose complex commands, allowing the output of one command to be passed as input to another. A pipeline is set up by piping the output of one command (or pipeline) to another command, using the | operator. But, unlike its Unix counterpart, the PowerShell pipeline is an object pipeline; that is, the data passed between cmdlets are fully typed objects, rather than character streams. When data is piped as objects, the elements they encapsulate retain their structure and types across cmdlets, without the need for any serialisation or explicit parsing of the stream, as would be the need if only character streams were shared. An object can also encapsulate certain functions that work on the contained data. These also become available to the recipient command for use. For the last cmdlet in a pipeline, PowerShell automatically pipes its output object to the Out-Default cmdlet, which transforms the objects into a stream of format objects and then renders those to the screen.

There are four categories of PowerShell commands:

  • Cmdlets, which are .NET programs written by a developer and compiled into a dynamic-link library and loaded by a PowerShell script,
  • PowerShell Scripts (files suffixed by ‘.ps1’),
  • PowerShell Functions,
  • Native Windows commands (executable programs).

PowerShell Cmdlets

Cmdlets are specialised commands in the PowerShell environment that implement specific functions. These are the native commands in the PowerShell stack. Cmdlets follow a – naming pattern, such as Get-ChildItem, helping to make them self-descriptive. This standard simplifies the learning curve and provides a better description of what the cmdlet does. To see a list of cmdlets available in PowerShell type the following cmdlet:

Get-Command

To see all cmdlets starting with get- type the following command:

Get-Command get-*

Cmdlets output their results as objects, or collections thereof (including arrays), and can optionally receive input in that form, making them suitable for use as recipients in a pipeline. But, whereas PowerShell allows arrays and other collections of objects to be written to the pipeline, cmdlets always process objects individually. For collections of objects, PowerShell invokes the cmdlet on each object in the collection, in sequence.

Getting Help

In learning new technologies, it is important to find information quickly and easily. Windows PowerShell includes its own extensive, console-based help, similar to man pages in Unix shells via the Get-Help cmdlet. This will be the most utilised cmdlet until you become more proficient.

Main cmdlet for getting help is:

Get-Help

Information about all available help topics:

Get-Help *

Information about a specific cmdlet:

Get-Help Write-Host

Two other forms of the Get-Help cmdlet exist, the noun Help and the -? parameter. Help, by itself provides the same info as Get-Help *.

No comments yet.

Leave a Reply

revia in alcoholism