Now let’s get started with Cmdlets, the essence of Windows PowerShell.

Understanding PowerShell Cmdlets

Taking advantage of the power of PowerShell is done with the use of cmdlets. The cmdlets are powered by a set of .NET objects and methods developed with server and network administrative purposes in mind.

An example of a cmdlet is Get-ChildItem, which is used for listing the contents of an object. That’s right, object.

Any output created within PowerShell is output in the form of an object as opposed to text. An object is an item that can contain multiple properties and/or methods, which can return more objects.

If no action is performed on an object, PowerShell outputs the object details in text form. Actions can be performed on an object returned by a cmdlet by pipelining another command with the pipe character between commands.

The command below lists the contents of the current object (directory) sorted by name in descending order.

get-childitem | sort-object “Name” –descending

In the above example, the Sort-Object cmdlet is used on the return object of the Get-ChildItem cmdlet to sort by the Name property. Tacked onto the end of the command are the property and descending parameters.

The actions performed by cmdlets are determined by the parameters supplied to the cmdlet. Alternatively you could have supplied the –property parameter flag before “Name”, but because that is the cmdlet’s default parameter, that is not necessary.

Aside from the cmdlet specific parameters, you can also use common parameters with cmdlets to be handled by the PowerShell engine itself.

Not all cmdlets support all the common parameters; most of them usually work though. The common PowerShell parameters are defined below:

-confirm – Prompts the user for confirmation before executing a command.

-ErrorVariable – Tells the cmdlet to assign error information to the variable specified.

-OutBuffer – Tells the cmdlet to hold a certain number of objects before calling a pipelined cmdlet.

-OutVariable – Tells the cmdlet to assign output to the variable specified.

-verbose – Tells the cmdlet to provide more detail.

-whatif – Tells the result of executing the command without executing it.

If you have been working along, you probably noticed that the output from the Get-ChildItem cmdlet looks a lot like what you would expect to see by entering dir. This is no coincidence.

When you enter dir you are actually entering the alias assigned to the Get-ChildItem cmdlet. Cmdlets come with pre-assigned aliases and can also have custom defined aliases.

Cmdlet aliases can be listed with the Get-Alias and set with the Set-Alias cmdlets.

Should you ever have the need to find out more information on a cmdlet you can use the Get-Help cmdlet. The Get-Help cmdlet itself has parameters such as –detailed and –full for determining the amount of help shown.

If you ever have trouble remembering the name of a cmdlet you can use the Get-Command cmdlet. Entering Get-Command by itself lists all the PowerShell cmdlets. You can also supply part of a cmdlet name to narrow down the results like in the example below:

get-command get-

Executing the above command lists all the cmdlets that begin with ‘get-‘. The Get-Help and Get-Command cmdlets should prove very useful for helping you to get familiar with the capabilities and usage of PowerShell.

Understanding PowerShell Providers

By default, when PowerShell is launched, you will start with your profile folder as your working directory. Access to this directory is available to you courtesy of the FileSystem provider.

PowerShell providers take the form of logical drives that provide access to data stored within the drives in a fashion that can be navigated and manipulated using the PowerShell cmdlets.

PowerShell comes packaged with seven default providers. You can list the available providers and their respective drives with the Get-PSProvider cmdlet. Additional providers can be installed with the New-PSDrive cmdlet.

The default PowerShell providers are explained below:

Alias – Provides access to the aliases assigned to cmdlets.

Certificate – Provides the ability to sign and work with scripts.

Environment – Provides access to the Windows Environment variables that hold information on configuration and special folders.

FileSystem – Provides access to the files and folders available on your local drives.

Function – Provides access to the functions defined in PowerShell.

Registry – Provides access to the Windows registry.

Variable – Provides access to the variables assigned in PowerShell.

You can gain access to the different PowerShell providers by using the Set-Location cmdlet. The command below will change the working directory to the Variable provider.

set-location variable:

From the Variable provider you can list all the variables assigned in the console and their respective values with the Get-ChildItem cmdlet. You can also create new variables using the New-Item cmdlet or remove them with the Remove-Item cmdlet amongst other things.

It’s important to note that the Variable provider is not the only place you can work with variables. Variables can be assigned or used anywhere you go with PowerShell.

Variables are identified in the PowerShell console with the dollar sign. Executing the command below will assign a value to a variable.

$variable = “I’m a variable”

To display the newly assigned variable, execute the command below.

$variable

Providers give great potential for expanding the capabilities of PowerShell. They provide access for working with many resources on the server.

The PowerShell provider system has removed the restriction of working solely with the server’s file system within the command shell environment.

We’re done for today, but don’t forget to come back next week for more on taking command of Server 2008 with Windows PowerShell!