I’ve been running through the book Learn Windows PowerShell 3 in a Month of Lunches, by Don Jones and Jeffrey Hicks, widely considered one of the best books for PowerShell beginners. My “curriculum” largely follows the chapters in the book, each meant to be concentrated on for about a “lunch” worth of time.
Today, starting this off, I get into the Help System. We’re going to figure out how to search for a specific service running on your own computer.
The Importance of the Help System
PowerShell commands are typically called cmdlets. PowerShell has a lot of cmdlets, and with each new version comes even more cmdlets.
I’m using a Hyper-V virtual machine with a Windows Server 2012 R2 install. I found a command to return how many cmdlets are loaded into my install of Powershell:
Get-Module -ListAvailable | Import-Module ; gcm -co cmdlet | measure
On my Server 2012 VM, there are 651 cmdlets installed. On my Windows 8 VM, there are 837! That’s insane; just way too many to remember. Thankfully, PowerShell is built with this in mind, and comes with a great system for finding commands and searching for help.
Searching For PowerShell Commands
Let’s say you want to find the services available on your Windows 8.1 PC, without using Task Manager. However, we don’t know which PowerShell cmdlet will return a list of services. So let’s just try this for the heck of it:
Nice — a whole list of cmdlets with “Service” in the name. One of them is Get-Service. That sounds like what we need!
NOTE: The terms you use for Get-Help accepts wildcards as well, which can help you narrow results down further.
PowerShell cmdlets are in Verb-Noun format. Often it’s pretty easy to track down a command you don’t know the name of, just by searching with a verb and a noun; the names are pretty intuitive. On the above list you can see New-Service, Restart-Service, Resume-Service, Start-Service, and Stop-Service. All of these do just what they say.
Now we think Get-Service will do the trick, but how do we use it? We can use Get-Help to get more information on a specific command too:
The resulting screen is called the Summary Help for this particular cmdlet, and gives an overview of what this cmdlet does, as well as the syntax use, including which parameters are acceptable for this cmdlet.
Finding Examples of How a Cmdlet is Used
I’ll get more into command parameters later. For now, see at the bottom, “Remarks” section of the summary help: We can see more examples by adding -examples to this command. So let’s see what comes up when we do:
In this case, I have to scroll up to see the rest of the 11 examples it spat out. This has been one of my primary ways to learn PowerShell: Get curious and find commands, then use the examples to figure out common use scenarios.
Example 1 shows that if I only need to get the list of services on my own PC, Get-Service by itself, with no parameters, will work.
The above screenshot shows a small snippet of the output of Get-Service. It returns a list of services in alphabetical order, listing its Display Name and whether or not the service is running.
In Example 8 of the Get-Service examples, we see that we can sort the results by column. For example, let’s sort by Status so we can see what’s running a bit easier:
Get-Service | Sort-Object status -descending
The above snippet shows that the services are now sorted, with the Running services first, followed by the Stopped services.
Maybe I want to find a specific service. What service on my Server 2012 machine runs Group Policy? Let’s try filtering by a Name with “Group” anywhere within it:
Get-Service -Name *group*
Hmm, nothing; it came up empty.
Okay, but as we look back at the list of Services, their Display Names are much more descriptive than many of the service names, which are often abbreviations or acronyms of some sort. Let’s perform the same search, but using -DisplayName instead of -Name:
Bingo, one result returned. There’s a gpsvc service, which we wouldn’t have found intuitively unless we already knew the name. However, since we could search by Display Name, finding a Group Policy-related service wasn’t too hard.
TIP: Using Google and TechNet to Search For Help
If you find yourself referring back to a certain cmdlet’s help screen, you can Google the command. Microsoft was smart with how they implemented this with searchability in mind — the top Google result for Get-Service is the official TechNet documentation on Get-Service, which should look pretty familiar, as it’s almost exactly the info you could bring up from within PowerShell.
I prefer to get help via TechNet instead of the PowerShell console when I’m just learning a command, since the page shows all of the examples for easy reference.
Next Time: More on How Commands Are Run
The next study session will go a little bit deeper into the syntax, including how to interpret the Syntax section of each cmdlet’s summary help. Until you know how to interpret the codey-looking stuff in the syntax section, that section won’t make much sense.