Take Command of Server 2008 with Windows PowerShell – Part 2By Jason Ensinger
Welcome back! Today we’re learning about PowerShell Cmdlets and PowerShell Providers.
If you missed the first part of this short series, make sure to take a look at it when you get a chance.
Take Command of Server 2008 with Windows PowerShell – Part 1 will provide you with a good introduction to PowerShell and show you the steps to installing and accessing PowerShell on your Server 2008.
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.
-debug– Tells the cmdlet to provide debugging information.
-ErrorAction– Allows the cmdlet to; continue, stop, silently continue or inquire when an error occurs.
-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
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
–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:
Executing the above command lists all the cmdlets that begin with ‘get-‘. The
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
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.
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.
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!
About the Author
Jason Ensinger (A+) is experienced in both IT and development. He has completed training in computers, electronics and networking and obtained his A+ certification. Jason is a self-taught developer and over half of his career in technology has been in web and Windows development, while the rest has been IT orientated. He hopes to be able to use his cross industry expertise to be able to shed more light on the exciting life of a developer for those in IT considering making the move to software. Jason has written articles on various topics including SharePoint, CompTIA A+, and Windows Server 2008.