Managing Services the PowerShell way – Part 1: Get Service status

In this comprehensive series you’ll learn all you have to know about managing Windows Services with PowerShell 2.0 and 3.0. Part 1 discusses how display services on local and remote computer.

Jeffery HicksMVP By Jeffery Hicks - Tue, January 15, 2013 - 3 comments google+ icon

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with 20 years of experience. Follow his blog.

Articles in this series

PowerShell and Services

If you are new to Windows PowerShell, you may be afraid it is too difficult to learn. Or that you have write complicated scripts in order to use it. That couldn’t be further from the truth. PowerShell is designed as an interactive management engine that just happens to allow scripting. The truth is that you can use PowerShell from a text based console and manage your Windows environment with a few simple commands.

To prove this we’re going to look at managing services with PowerShell from a command prompt. The emphasis is on using PowerShell interactively, not trying to script. I’m going to assume you already have PowerShell running on Windows 7 or later and you have admin rights. If you want to follow along, please try to do so in a non-production or virtualized environment. I’ll cover service management using PowerShell v2 and v3.

Get service status

Let’s begin by simply getting the status of all the services on the local computer. To accomplish this we’ll use a command called Get-Service. A PowerShell command like this is referred to as a cmdlet.

PowerShell is generally not case-sensitive. You can see the results in the screenshot below.

Manage Services with PowerShell - get-service

Manage Services with PowerShell – get-service

The output looks text but each line is actually a service object. This object-orientation in PowerShell is what trips up many beginners. All we see here is the default display. But each service object has other properties as well. You can discover them by piping the objects to another command, Get-Member.

You can see the results in the next screenshot.

Manage Services with PowerShell - get-member

Manage Services with PowerShell – get-member

The Typename at the top of the screen tells us what type of object this is, a System.ServiceProcess.ServiceController. When you see a name like this that starts with System, you will know that it is a .NET object. This isn’t too important right now but I wanted you to understand how to discover the object type. I’ve also circled the object properties. These are attributes that describe this type of object. Even though many of these aren’t used in the default display, if you know them you can use them.

For example, let’s say we are only interested in the Windows Update Service. We can ask Get-Service to display just that service and then select a few properties.

How did I know I could type a service name? I looked at help for Get-Service.

The screenshot displays the short help. I’m running this in a PowerShell v2 session on Windows 8 so you might see slight variations on older systems.

Manage Services with PowerShell - help get-service

Manage Services with PowerShell – help get-service

You can get complete help by typing:

Or, if you are more comfortable with display names, use the –Displayname parameter.

I had to use the parameter name so that PowerShell would treat the values as a display name and not an actual service name. A command like this would fail:

Because the –Name parameter is positional, meaning you don’t have to type it.

Service status of remote computers

So far we’ve been looking at service information on the local computer. Naturally we want to manage services on remote computers. If you look at help for Get-Service you’ll see a –Computername parameter. Connecting to remote computers like this does not use PowerShell’s remoting feature nor does PowerShell remoting have to be enabled anywhere. If you can manage services using command line tools like SC.EXE or the Service Manager management console, you can use PowerShell. Let’s try it out:

Any command I’ve shown you so far you can use to query a remote computer. Even multiple computers, assuming your credential has sufficient administrative privileges on the remote computer. If you have PowerShell v3, it is very easy to check a single service on multiple computers.

Although you need to format the output slightly to know which service goes with which computer.

That’s a one line command that queried the service on 3 different computers and displayed the results in a nicely formatted table and you didn’t write a single script!

To do the same thing in PowerShell v2 takes a slightly more awkward approach, but still no scripting.

Next time we’ll look at another way to accomplish this and other filtering techniques as well as explore more of this service object.

Series Navigation - Managing Services the PowerShell way – Part 2: Filtering

-1+1 - Rate this post
Loading ... Loading ...
Your question wasn't answered? Ask in the new 4sysops forum!

3 Comments- Leave a Reply

  1. Hans says:

    Thanks Mr. Hicks! Exactly the info I needed!

  2. We have used the below Powershell script that sends html status of services

    http://msexchange.me/2013/07/07/services-monitor-report/

  3. Vikas, a few comments on your script since you suggested it as a link. I think you are working way too hard. There is no need to manually create the html file. That is what ConvertTo-HTML is for. There is a also a cmdlet, Send-Mailmessage, for email the report. The rule should be to always look for cmdlets to get the job done.

    There are many ways to create reports with embedded style sheets which is what you are trying to do. If you check my blog and search for ‘html’ you’ll find a number of examples.

    http://jdhitsolutions.com/blog/?s=html

===Leave a Comment===

Login

Lost your password?