Introduction to Cmdlets

Introduction to Cmdlets

For years Microsoft has played a trick on systems administrators. They convinced you that you could administer Windows Servers at the same scale that Linux administrators could. While that sentiment is true the tooling wasn’t always geared towards system administrators. You had MS-DOS and batch files that you could use to automate anything you could do at the command line as well as VB scripts for more programmatic automation. The trouble is, batch lacks an interface into most of the underlying OS and VB is a full-blown programming language. When Microsoft released PowerShell in 2006 they aimed to cater to those who wanted more from batch and less “dev stuff” than VB.

Jeffrey Snover envisoned the console and scripting language built on top of Microsoft’s .Net Framework and could extend the .Net Framework. Objects created in PowerShell were .Net objects. But PowerShell is more than just a console application. Microsoft has opened up the underlying OS to PowerShell in a way it never has before for the operations team.

PowerShell Cmdlets (short for CommandLet) center around the Unix Philosophy of Do One Thing and Do it Well. The Start-Process cmdlet only starts a process. Stop-Process only stops them. There isn’t some overarching process command that does both. PowerShell abstracts the .Net, WMI, and C++ away so you don’t have to care how the process is created. A PowerShell cmdlet does one thing, and one thing VERY well.

Naming Conventions

Cmdlets come in many types. They deal with different object types like stored credentials, ip addresses, strings, numbers, and many more but the conventions stay the same. Cmdlets follow the Verb-Noun naming convention. The nouns are singular even though they can output more than one object. The Cmdlet names also follow the Pascal capitalization style where every word is capitalized.

These naming conventions make it easy to find new commands. Looking for everything dealing with services?

[code lang=powershell]
Get-Command -Noun Service
[/code]

You now get back a whole list of cmdlets that work with services. The same works for searching for verbs.

[code lang=powershell]
Get-command -Verb write
[/code]

You can now see all cmdlets that can write. Both can be used with wildcards.

[code lang=powershell]
Get-command -Verb write -Noun out*
[/code]

Approved Verbs

When writing functions you are required to come up with a new for the function. It comes from the function declaration.

Function New-Cmdlet { param( $Verb, $Noun ) Write-Output ‘$Verb-$Noun’ } New-Cmdlet -Noun Cmdlet -Verb New

New is a verb approved by Microsoft to run in PowerShell. Here is how you get a list of approved verbs.

[code lang=powershell]
Get-Verb | Out-gridView
[/code]

We pipe the list to out-gridview so we can sort by name or by group. Stick with these verbs when writing functions. It will make discoverability of your functions easier for people who work with your functions and will work perfectly when you bundle up functions into a module.

Googleability

Googleability is a made up word. Made up, yet we you know exactly what I mean. How easy is something to google. When I search for Microsoft Virtual Academy on Google using the search term ‘MVA’ I get a bunch of sites I’m not looking for. I want something specific and am forced to search by the full name of the site. The results I want are hidden in noise.

The word cmdlet doesn’t mean anything besides PowerShell CommandLet. This makes searching for online documentation of cmdlets simple. Simply search on “cmdlet Write-Information” to get exact hits on what you’re looking for. I’m not sure if this was by design but it certainly makes finding help easy online.

4 thoughts on “Introduction to Cmdlets

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.