Windows Server How-To
How To Use PowerShell To Manage System Services
Being able to call up specific variables is key in narrowing down a large data set.
Ever since Windows Server was first introduced back in the '90s, the tool of choice for managing system services has been the Service Control Manager. It is possible, however, to manage system services through Windows PowerShell. In this post, I will show you how it's done.
If you want to see a quick overview of service status, then you can easily display system service information by entering the Get-Service cmdlet. You can see what this looks like in Figure 1.
As you can see in the figure above, the list of services can be really long. If you are interested in a specific service, then you can cut through the clutter by appending the service name to the Get-Service cmdlet. For instance, you might type Get-Service DHCP.
Another way that you can use the Get-Service cmdlet is to create a list of the services that are running (or the services that are currently stopped). To do so, you will need to use the Where command and examine the service status. Suppose, for instance, that you wanted to see all of the running services. You could do so by using this command:
Get-Service | Where {$_.status –eq 'running'}
You can see what this looks like in Figure 2.
So what if you want to start or stop a service? Well, PowerShell makes that easy enough to do, but you are going to have to open an elevated PowerShell window. Otherwise, you will get an error message when you try to stop a service.
To start or stop a service through PowerShell, you can use the Start-Service or the Stop Service cmdlet, followed by the name of the service that you want to start or stop. For instance, you might enter Stop-Service DHCP or Start-Service DHCP. You can see an example of what it looks like to stop and then start a service in Figure 3. Incidentally, the Get-Service cmdlet that you see in the figure isn't required. I just included it as a way of verifying the service status.
You might have noticed that in the text above, I mentioned using PowerShell to stop the DHCP service, and yet Figure 3 shows me starting and stopping the BITS service. The reason for this inconsistency is because Windows considers DHCP to be a dependency service. If you want to use PowerShell to stop a dependency service then you have to do things just a little bit differently. You are going to have to append the –Force parameter to force the service to stop.
Using PowerShell to manually start and stop system services is all good and well, but you can take things a bit further. Suppose, for instance, that you have an application that is less than reliable, and you need to periodically confirm that the application's services are running. You could create a PowerShell script to check the service status and then start the service if it has stopped. Such a script could be included as a function in a user's logon script or could be scheduled to run periodically.
The actual code that you would use is really simple. In fact, you can check a service and start it if necessary by using a single line of code. Suppose that I wanted to check to see if the Wsearch service was running, and start the service if it is not already running. The code used for doing so would be:
Get-Service Wsearch | Where {$_.status –eq 'Stopped'} | Start-Service
You can see how this works in Figure 4. Once again, I have added a couple of Get-Service lines just so that you can see what is going on with the service as its state changes.
One last thing that I want to mention about services is that the information that PowerShell displays when you enter the Get-Service cmdlet is not the only information that is available. If you would like to see more detail about a particular service, you can use the Select-Object cmdlet, followed by an asterisks. For instance, if you wanted to know all about the BITS service, you could use this command:
Get-Service BITS | Select-Object *
You can see the output from this command in Figure 5.
About the Author
Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.