Powershell UserDefined Cmdlets

Powershell statements are "commands" to the powershell console.
The purpose of the statements is to tell the powershell what to do.
Command to get powershell version

Execute below command to know powershell version

Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1

Note: You can identify version at the top powershell window.

Windows PowerShell Cmdlet

Cmdlets are specialized commands in the PowerShell environment that implement specific functions.
Below four cmdlets are critical to figuring out how to make PowerShell do your work.

Cmdlets - Get-Help
Cmdlets - Get-Command
Cmdlets - Get-Member
Cmdlets - Get-PSDrive

Command to get powershell version

Execute below command to know powershell version

Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1

Note: You can identify version at the top powershell window.

The concepts in this tutorial have come from my readings of Bruce Payette's book - Windows PowerShell in Action. Bruce is a founding member of the PowerShell team and is a co-designer of the PowerShell language. His book is highly recommended and has been made available from this site. Who better to learn about PowerShell than from the designer himself!

There are four categories of PowerShell commands: Cmdlet (Command-Let), PowerShell Functions, PowerShell Scripts, and native Windows commands. Each category will be examined in tutorials on this site. Lesson 2 focuses on the PowerShell cmdlet, so let's get started...

cmdlet - naming convention

There are over a hundred new PowerShell commands to learn, but no need to panic. Unlike most other command-line interfaces, PowerShell commands have been standardized using a "verb-noun" naming convention know as a cmdlet. This standard simplifies the learning curve and provides a better description of what the cmdlet does. To see a list of cmdlets available in PowerShell type the following cmdlet:

get-command <enter>

Note: <enter> denotes typing the Enter/Return key on your keyboard.

Image 2.1

The "Name" column lists the cmdlets in the "verb-noun" naming convention.

Next, let's list all the commands that use a specific verb. The following command yields all cmdlets that use the verb "Get."

get-command -Verb Get <enter>

Get-Command -Verb
Image 2.2

Play around with the -Verb parameter, try finding commands that use the verbs; Add, Clear, New, and Set.

Getting commands that use specific nouns is just as easy. Type the following command to see which cmdlets use the noun "Service."

get-command -Noun Service <enter>

Get-Command -Noun
Image 2.3

Just as it sounds, the verb describes an action and the noun describes the "what" to take the action against. One thing I want to mention before moving forward, the PowerShell command line is not case-sensitive. In PowerShell - "get-service" or "Get-Service" or even "GeT-SerVIce" is the same command.

Getting help

In learning new technologies, it is important to find information quickly and easily. Get-Help cmdlet has been designed for that purpose; this will be the most utilized cmdlet until you become more proficient.

Get-Help Examples:
Information about Get-Help cmdlet. Includes description, syntax, and remarks.


Information about all available help topics.

Get-Help *

Information about a specific cmdlet.

Get-Help Get-Service

Two other forms of the Get-Help cmdlet exist, the noun "Help" and the "-?" parameter. Help, by itself provides the same info asGet-Help *. Use Help with a cmdlet as follows,Help Get-Service. With the help parameter, Get-Service -?. The Scripting Guys created a graphical Help file which is available on this site. Located in the Categories -> PowerShell Downloads -> PowerShell Graphical Help File. The Graphical Help file is great tool for learning and reference, I recommend downloading it.

Using cmdlets

In this section I will be using Service to demonstrate cmdlets. Image 2.3 lists eight cmdlets associated with the noun Service: Get-Service, New-Service, Restart-Service, Set-Service, Start-Service, Stop-Service, and Suspend-Service. Not only will we be running cmdlets from the PowerShell command line, I'm also going to explain it graphically. O.k. that sounds kind of graphic... what I mean is I'm going to describe the equivalent action taken when working in GUI tools. I find this a tremendous help when attempting to understand new concepts.

What services are on the computer? Type the following cmdlet:

get-service <enter>

Image 2.4

You should now have a list of services on the computer as well as their status, similar to the list above. Which GUI tool gives us the same information? The Services MMC (Microsoft Management Console). Launch the Services MMC from Start -> Programs -> Administrative Tools -> Services. If Administrative Tools are hidden from the Programs Menu, you can launch it by typing services.msc from the PowerShell command-line. As stated earlier, one of the PowerShell command categories is "native windows commands." This is how we are able to launch services.msc from the PowerShell command-line.

Services MMC
Image 2.5

Exercise 1: Stopping and Starting a Service.

For this exercise I will be using both the Stop-Service and Start-Service cmdlets. To demonstrate, I have chosen the "Computer Browser" service for this example. The current status of the Computer Browser service is "Started" and the "Status Type" is "Automatic." For this exercise it is important that you choose a service that has a status type of either automatic or manual. If you choose a service with a status type of disabled, this exercise will not work.

The following syntax stops a service:Stop-Service [-name] or Stop-Service [-displayName]. This is not the complete syntax for the Stop-Service cmdlet. If you want to see the full syntax, use the Get-Help cmdlet as discussed earlier. To begin, let's show the status of the Browser Service. Then Stop it using the Stop-Service cmdlet. Lastly check the status. Type the following cmdlets:

Get-Service -name Browser <enter>
Stop-Service -name Browser <enter>
Get-Service -name Browser <enter>

Stop-Service cmdlet
Image 2.6

Note: When you run the Stop-Service cmdlet there is no indication that the service has actually stopped. This is why I ran the Get-Service command to verify the status changed to "Stopped."

We should see the same change in the Services.msc GUI. In the GUI a Status of "Blank" means "Stopped."

Stop-Service cmdlet GUI
Image 2.7

The following syntax will start a service:Start-Service [-name] or Start-Service [-displayName]. Look familiar? Again, let's check the status, start the service, and verify the status has changed.

Get-Service -name Browser <enter>
Start-Service -name Browser <enter>
Get-Service -name Browser <enter>

Start-Service cmdlet
Image 2.8

Check the GUI, the status of Browser should be "Started" (make sure to refresh the view).

This exercise should give you an indication of the benefits of using PowerShell. With just a couple of cmdlets we were able to stop and start a service. Traditionally, an Administrator would launch the GUI, select the service, and then either stop or start it. Doesn't seem like a big deal until you are asked to stop a service on multiple computers. Through PowerShell Scripting you will be able to start, stop, and change status types of a service on multiple computers without much effort. Write a script, run-it, and then kick you feet up on the desk.

This tutorial has introduced the PowerShell naming convention, cmdlets, getting help, and an example of using the Service cmdlet. As stated before, there are over a hundred cmdlets in PowerShell. This should be enough to keep you busy for awhile. See you in the next tutorial where we will be covering PowerShell Aliases.