Intro to PowerShell 2 – Cmdlets

Path Validation and Creation 101

In part 1 of this series we were introduced to the PowerShell CLI and ran the Get-Help command. In this part we will introduce you to Cmdlets.

Introducing Cmdlets

Cmdlets (pronounced command-lets) are specialised commands used in the PowerShell environment to call specific functions. To put this another way, Cmdlets are simple ways of calling and executing the pre-built commands in PowerShell.

PowerShell Cmdlets are easily identifiable because they use binomial nomenclature – ie there are always 2 parts to the name and both parts are separated by a hyphen. For example: The Get-Help command that we used in Part 1 is actually a Cmdlet.

In part 1 of this series I mentioned that we could pipe the output of the legacy dos command DIR to a file with a single line entered into the PowerShell CLI. To do this we would enter the following text in the PowerShell CLI:

dir | Out-File C:\Utils\dir.txt

In plain English this says “Take the output of the dir command and give it to the Cmdlet Out-File to be written to the file dir.txt in the C:\Utils\ path. Pretty powerful stuff for a command that is only 31 characters long.

You might have noticed that there is a character in that command that you might not be familiar with. It’s the | character and on most US style keyboards it’s located on the same key as the \ key. The | character is what we refer to as the pipe operator. It tells PowerShell to take the output of what comes before and hand it off to what comes next. For more information on the | operator and the fun you can have with it, take a look at the activity below.

Cmdlets then are the building blocks of PowerShell these are the preconfigured parts that allow us to quickly and easily perform repetitive tasks that we might previously have performed manually, or written a pages long VBS or batch script to try to do.

In PowerShell, Cmdlets aren’t just limited to piping the output from one function to another. Cmdlets also let us interact with various API’s built into products like Windows Desktop and Server Operating Systems, Exchange Server, Hyper-V, Azure and many more. At last count there were well over 15,000 Cmdlets for Microsoft products alone. Companies have even built their own API’s and released Cmdlets to take advantage of those API’s as well.

Activity: Path Validation and Creation 101.

You can pipe a lot of information from one Cmdlet to another. I’ve often wondered, what’s the most interesting and useful thing we could do by piping the output of Cmdlet to another?

File listings with PowerShell are a great way to become familiar with PowerShell and to learn how truly powerful the pipe operator can be and some of the limitations of Cmdlets as well.

We’ll start by first revisiting the command we looked at earlier. If you haven’t run it already, open the PowerShell CLI as an administrator and enter the following command:

dir | Out-File C:\Utils\dir.txt

At this point, one of two things will happen. Either the PowerShell window will pause for a few seconds and then bring up another prompt or more likely it will bring up an error. That looks much like the figure below

 ITPS_B2_PSError

 If you read through the error text you’ll see that it tells you that it can’t find part of the pathway you’ve instructed it to use. One of the limitations of the Out-File Cmdlet is that it won’t validate the directory path and create it if it doesn’t exist. That function is handled by two other Cmdlets called Test-Path and New-Item respectively.

The command that we would run at this point are as follows:

Test-Path C:\Utils

 If the path C:\Utils exists then Test-Path will return a value of True, otherwise it will return the value False.

We already know that the path doesn’t exist so we can move right along to the next command which is:

 New-Item C:\Utils -ItemType directory

Running this command will create a new item of the directory type and call it Utils. You can check your C: drive to make sure it has run properly if you like or you can simply re-run the first command:

dir | Out-File C:\Utils\dir.txt

PowerShell will now pause briefly while it lists everything in the current folder you are in.

Congratulations are in order because you’ve now run three separate Cmdlets and used them to validate a path, create the path that didn’t exist and then output the contents of a folder to that path. You’re well and truly on your way to getting to grips with how powershell works.

Outcomes

Today we learned:

  • Cmdlets are the building blocks of Windows PowerShell.
  • The pipe operator (\) allows us to hand off the output of one Cmdlet to another
  • Cmdlets can take advantage of API’s built into a wide range of software
  • Cmdlets are often limited in the scope of the function they perform.
  • Cmdlets can do everything we can do in explorer (and more than a few things we can’t)

About Aaron Milne

Aaron is a systems administrator from Australia. He specializes in SMB administration where he has become well known for his ability to find unorthodox but entirely workable solutions to problems. Aaron has a talent for building something out of nothing and has proven himself to be highly adaptable to new technologies.
Aaron is a regular contributor to WeBreakTech as well as an occasional writer for The Register.

Visit My Website
View All Posts