Windows PowerShell Cmdlet

PowerShell Tutorial 2: PowerShell Commands – Cmdlet


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.

Get-Command
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.

Get-Help

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 as Get-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>

Get-Service
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.

Email This Page To A Friend Email This Page To A Friend

Comments

By chris wood on May 2nd, 2008 at 2:53 am

Fab tutorial, I will be completing the rest of the series.

Top Dude

By Mike Scott on July 25th, 2008 at 11:35 am

Great tutorials so far! Thanks a bunch!

One suggestion though, you should put links at the bottom to jump to the next chapter. Thanks again!

Keep up the Good work.

Thanks MVP!

Great tutorial…Keep it coming..the link idea is a good one.

You can already start and stop services from the command line without Powershell. It’s “Net Stop [service]” and “Net Start [service]“. Put it in a batch file and push it out using your favorite app pushing service in Active Directory or eDirectory.

A good way to teach something new, is to relate it to something already known. Stopping and starting services is a great example.

Hi everyone,
i just found out something in the PowerShell console. Try this:
Type Get-Command -Verb G and now press *TAB*…The Word *Gentleman* appears!!!
Anyone has any idea what this is? An easter egg?

> Kiran : I don’t think so. Perhaps you have something named “Gentleman” in your working directory…

You might want to mention that on Vista we need to run Powershell as administrator for the service example to work unless i am missing something..

By Bingo Little on March 16th, 2009 at 2:48 am

“The Scripting Guys created a graphical Help file which is available on this site. Located in the Categories -> PowerShell Downloads -> PowerShell Graphical Help File.”

You should make use of HTML’s hyperlinking feature more often. It’s fabulous!

By Tim, From Australia on April 15th, 2009 at 3:14 pm

Great post,

Keep up the good work

Thanks

Hi

Thanks for the great efforts to teach us!
Appreciate it !!!!

Regards
Kristof

Thanks and thanks again,

Taking familiar tasks and showing how its done in powershell is a great idea.

Keep up the GREAT work!

By John Shannon on July 25th, 2009 at 7:37 am

Thanks for a great job. I do have a question: I’m running a beta of Win 7. the “start-service” and “stop-service” don’t seem to work, any idea what could be stopping this?

John, right-click on Powershell icon and choose to “Run as Administrator”. That might fix your problem. Unless you do that, Powershell console will be running with standard user privileges.

Thanks for this terrific guide.

I’m not a natural programmer, but I’ve made more progress going through this guide than I have in weeks through reading the usual books.

EXCELLENT stuff – many thanks!

Hi, this is very helpful tutorial – easy to follow and understand. Thank you! However, the TOC on the right does not work – when I click on the links from top (conditional logic) – then it jumps to tutorial#8, instead of tutorial #3. It would be helpful to have the link to the next tutorial at the bottom of each tutorial. Thanks again!

I have problem using following commands to find the status of service. For some of the services its not working
ex: get-service -name clipbook
get-service -name DHCP client

anybody have any idea why these cmdlets not working on my windows 2003.

I found the answer for my questions..
thank you
tutorial is awesome

This is a really excellent guide to PowerShell. You have a gift for explaining technical concepts. Well done, and please keep up the good work

Thanks for the tutorials. There aren’t too long and I do like the way you explain things.

Could not get the exercise to work with Windows7.

Hi,

Please specify what version of powershell the tutorial is for. Thank you.

Excellent PS tutorial for this beginner. Thanks

Works with Win7 if you start PS with RunAs Administrator

can we get the service status for a remote server?

hi,

its a gr8 tutorial and it is very helpful for beginers like me.

By royal charlie on October 22nd, 2010 at 3:57 am

Superb! cheers for a great tutorial.

Thanks for this great powershell tutorial. If you are a beginner, this is the place to start!

By José Carlos on November 17th, 2010 at 3:27 pm

thanks for the great tutorial

Wow! what a great article you wrote I’m just getting the feel of powershell.

Great tutorial. Very easy to understand. Seemed to have trouble opening my computer’s browser service though. It was stopped by default and when I tried to start it, got an error: Cannot open browser service on computer ‘.’. Perhaps it’s because my machine is so tightly managed. I’ll try some other services.

Great Tutorial. Great way to learn new skills. However, I am having problem stopping any service on my PC. Every time I tried , igot the following error message: Get-Service : Cannot find any service with service name ‘theme’.
At line:1 char:12
+ get-service <<<< -name theme
+ CategoryInfo : ObjectNotFound: (theme:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand"

Sorry for the long error message. But Can any one help me figure this out?

Cheers

Gerard , the service name should be themes not theme , with S :)

I want to create users from imported file of Excel and want to give the user name but I want to join last name and first name of user with just two character.
Example Hardik -first name
Patel – last name
I want to give HardikPa(this type of name ) to all users

Thanks so much for this, but i have a question, which is off the wall from the subject you have mentioned: how can I start more than one process? I’ve tried this:

start-service browser works, but start-service browser, branchcache didn’t work.

Like start-process notepad works, but start-process notepad, wordpad didn’t work.

Thanks so much in advance.

This is the best tutorial I’ve come across on this topic. Good job!!

By Rinson Antony on July 10th, 2011 at 10:38 pm

Power shell command to get a list of machines from an OU whose member of does not contain XYZ group.

The below command gives me the list of machine from the OU whose memberof is XYZ group.
Get-ADComputer -searchscope onelevel -SearchBase “OU=Desktops,DC
=test,DC=com” -filter {memberof -eq “CN=XYZ,DC=test,DC=com”}

I am trying to get the reversal of this result.. The result should give me result of machines whose memberof does not have XYZ group… Please help on this..

Thanks
Rinson

@Rinson
Get-ADComputer -searchscope onelevel -SearchBase “OU=Desktops,DC
=test,DC=com” -filter {memberof -neq “CN=XYZ,DC=test,DC=com”}

I had some troubles getting services to start and could not figure out why… Turns out I had to run PS as a Administrator (right-click, “run as administrator”) in Windows 7. Helpful tip for newbies like me.

I have a question though, for the life of me I cannot get the “fax” service to start through Powershell. I can start it in the GUI, and I can then also stop it PS. But I always get an error trying to start it. It is set as “manual” start -up type, not disabled. The error is as follows: Start-Service : Service ‘fax (Fax)’ start failed.
At line:1 char:14
+ Start-Service <<<< fax
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
ServiceCommandException
+ FullyQualifiedErrorId : StartServiceFailed,Microsoft.PowerShell.Commands.StartServiceCommand

Any help in why I get this?

Amazing tutorial thus far. I figured it was time to learn this, and I thought I would hate it. However, this tutorial is engaging, which is key in any learning process.

Should have seen this tutorial back in 2008! But as they say, it’s always never too late to learn ;)

Now I can upgrade my knowledge on VBScripting, WSC and WSH.

Good Job! Author.

I enjoy the way you keep things simple to understand without getting to technical. This allows one to ease into the subject and gain confidence. You are a good teacher.

Wow thanks for the help. Its very help full to work with service in non english build.:D

Are there universal switches other than
“-?” that work with cmdlets? Similar to “/p” (for pause) or “/o” (for order)?

is there a way to do a Get-service and filter only those that are either “running” or “stopped”?

excellent article.. Gave me a great introduction to cmdlets which other sites dint provide. thank you.

Great site! I just started a job in IT and I had never even heard of Powershell.

Thank you so much for all you have done!

-Sean

Thanks! I’ll check out Bruce Payette’s book, Windows PowerShell in Action.

Tam-
I tried with a pipe in between and it worked. start-process wordpad|notepad
Regards,
Tracy

I was able to use stop-service browser and start-service browser without using -name before browser. Should I continue using -name or can I go without since it works and it’s less typing?

Running W7 64 bit you may want to set the properties of the shortcut under advanced to run as administrator. You will be prompted to approve. But in this way you don’t forget it

Gerard

By PowerDave on April 5th, 2012 at 6:58 am

Louise asked about getting status of services (those stopped or started).
Here it is (found by using powershell help
Started services
get-service | where-object {$_.Status -eq “Running”}
Stopped services
get-service | where-object {$_.Status -eq “stopped”}

I found you could do get-help get-service and it returned an option to view examples. Check it out:
get-help get-service -examples

Enjoy!

Dave

RUN AS ADMIN FOR START OR STOP OF SERVICES
——————————————

Adding to what gerard has said,

If your OS is WIN 7(64bit) and if you want to start/stop services,

1) U MUST run your powershell in Administrator mode. If you do that, what happens is that…
2)Your home directory which is currently “C:\Myscripts” gets changed to “C:\Windows\System32″. Do your experiments there.
3) To get back to your original home directory I.e.”C:\Myscripts”,all you need to do is to close ur admin session and reopen the powershell shortcut:)

NOTE: This works only if you have NOT enabled “Run as administrator” from the properties.

For temporary admin rights, right click on the “powershell shortcut” and select “run as admin”, Doing this makes you run powershell with admin rights for a session only.

By Tamara D on May 1st, 2012 at 5:40 pm

I was able to stop and start. However when I did the get services after stopping services power shell did not update to stoped sevices. The service GUI did change to stop

By Sli,gitz on May 4th, 2012 at 2:36 am

great tutorial.Keep up the good work.

Using the shortcut to c:\MyScripts I was unable to stop the browser unless I ran as administrator and used -force

PS C:\Windows\system32> stop-service -name browser -force
PS C:\Windows\system32> get-service -name browser

Status Name DisplayName
—— —- ———–
Stopped browser Computer Browser

PS C:\Windows\system32>

then it took me to c:\windows\system32 ??

Great stuff, just a comment, you had me stumped on stopping a service, didn’t mention you must run as administrator when starting powershell to do this….

This:
Launch the Services MMC from Start -> Programs -> Administrative Tools -> Services
Should probably be:
Launch the Services MMC from Start -> Control Panel -> Administrative Tools -> Services

Great Stuff!!Keep it up
but would like to ask wen I m using command :new-Item it askfor entering values for the following parameters:
Path[0]:
Path[1]:
Path[2]:
..
..
How can i stop entering values

Great Article…. thanks mate.. I am little late to jump on the powershell bandwagon.. but his is really very helpful…

By Chelsea A on March 9th, 2013 at 11:14 am

It… It doesn’t stop… I keep getting an error saying that it cannot “open Browser service on computer ‘.’.

Any help here? I did the syntax exactly how you showed it here, and even used get-help to see if mine was different somehow….

Chelsea, did you run Powershell as Administrator? That might be what your are having problem with.

Following Error Occured for Start-Service -name Browser command

Start-Service : Service ‘Computer Browser (Browser)’ cannot be started due to the following error: Cannot start service Browser on computer ‘.’.
At line:1 char:14
+ start-service <<<< -name Browser
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand

By Sameer Khan on June 13th, 2013 at 1:39 am

Hey Naren,
start PowerShell with Administrator Rights.

With AdministratorRights I can start/stop windows update, for example.
But the Browser status is always “Stopped”.
Any idea how to fix that?

Need to have UAC disabled to stop services in Windows 7 Home Premium

When I am trying to start the service browser , I am getting below error, I have checked that it is automatic mode in services.msc

Start-Service : Service ‘Computer Browser (browser)’ cannot be started due to the following error: Cannot start service
browser on computer ‘.’.

At line:1 char:14
+ Start-Service <<<< -name browser
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand

By Allan Morris on August 28th, 2013 at 2:43 am

I have just moved into 2nd line service desk work and am finding this powerful tool so useful, and this tutorial even more handy! Excellent.

Topic was nicely explain.I have query that i want check sql reporting services for different instance.how to check ?

It’s a mixed bag for me. I can start some programs with no problem, cannot start or stop anything.

Is there settings in my computer stopping this?

Thanks

FYI – the get Verb and Noun examples are erroneous. After several attempts and still not successful in getting it to run I looked it up and this is what worked for me. get-command -verb get and get-command -verb add

How can I use POwerShell to automate .Net deployments?

This is a great introduction to PowerShell, especially for those of us that are new to the program. Thanks for sharing!

All who gets error when starting service: you need to: set-service browser -startuptype automatic before you trying to start it.
Administrator rights and UAC have nothing to do with it.

Great introduction to the get-service command. Thanks for sharing.

Great second article

Thank you for such a great article! I’ll be picking up the book by Bruce Payette.

you made powershell easy ..Thanks

Thanks a lot of sharing your knowledge! PowerShell seems easy to me now.

By Guilherme on July 18th, 2014 at 8:39 am

Starting WordPad from cmd:

write

Starting WordPad from powershell:

start-process write

Superb explaination ,please keep a link for next chapter

I also when it comes to services the -Name is different to the -DisplayName so if you have multiple say Exchange services. You might want to use something like:

Get-Services -DisplayName Exchange*

This will list them all. Where as -name would only list the services where the SERVICE NAME has Exchange in it.

To see the SERVICE NAME you will have to right click select properties.

 

Leave a Comment