Cmdlets are a powerful cmdlet set that allow you to do more with your PowerShell commands than ever before. cmdlets can be used to automate tasks, manage files and folders, and more. To get started with cmdlets, you first need to create a new PowerShell instance. To do this, type the following command into the PowerShell prompt: New-PSSession -Name “MyNewSession”


If you have used ipconfig or ping through the command prompt, you’re halfway to becoming a PowerShell ninja. So come on and join us as we discover cmdlets in this installation of Geek School.

Be sure to check out our first article introducing PowerShell, and stay tuned for the rest of our series all week long.

The Anatomy of a Cmdlet

In the first part of the Series we saw a cmdlet that looked something like this:

PowerShell cmdlets have a Verb-Noun syntax, which can be seen above. The important thing to note is that the noun is always singular even though the cmdlet might return more than one result. To see a list of legal verbs in PowerShell you can use the Get-Verb cmdlet.

Knowing the legal verbs and remembering the singular noun rule really helps guessing cmdlet names. For example, suppose you want to get a list of services and their status – that’s right, its as easy as Get-Service. How do you think we would get a list of running processes – that’s right, Get-Process will do the trick.

This makes it really easy to work with any technology. For example, if you had the Exchange cmdlets loaded, we could easily get a list of mailboxes on the server by using:

However, there is an exception. Exchange aside, all other technology specific commands will require a prefix. For example, if we wanted to get the users that are currently logged on via Remote Desktop, we would type:

Which can be seen in the screenshot below.

Note: This screenshot was taken on a Server 2012 box as that is where you are going to find most of the technology specific modules.

A while ago, I read an article by Don Jones, the Godfather of PowerShell, where he explained that Exchange shipped before this prefix was added to cmdlets, so it never implemented them and never will.

Aliases

Another feature PowerShell comes with is the ability to have multiple ways of running the same command – aliases, if you will. The awesome thing about them is that they included many commands you might have been using in the command prompt, as well as some Linux aliases. For example, in PowerShell we can get a directory listing by using:

Used to using the command prompt? Don’t worry, they have you covered.

Have a Linux background ? They have you covered there as well.

When you have been scripting for a couple of years, you tend to get lazy and start to use aliases a lot , but this doesn’t help newcomers who are going to read our code. To see what command an alias is running under the hood, you can use the following:

On the other hand, if you feel it’s time to step your game up, you can use the definition parameter to get all aliases for a cmdlet:

If you come from some other background, you can add your own aliases by doing the following:

Obviously, you will need to replace “icanhazfilez” with the name of your new alias and Get-ChildItem with the cmdlet you want it to run under the hood.

One thing to note is that you lose all the aliases you have defined when you close the shell. You can get around this by adding their definition to your profile script.

Truncating Parameters

Windows PowerShell also allows you truncate parameter names up until the point where they become ambiguous, that is to say up until the point where PowerShell can no longer figure out which parameter you are talking about. For example:

Is the same as:

If you by any chance make the parameters names too ambiguous you will get an error.

Legacy Commands

Finally, the commands you know and love will still work in PowerShell.

Just remember that legacy applications like ping output a string, and there is often a better way to do the same thing using a PowerShell cmdlet.

Instead of outputting a long string of text, we are now left with an object, which we will take a look at in tomorrow’s edition of Geek School.