Manage Learn to apply best practices and optimize your operations.

How to manage Windows services using PowerShell

Administrators can control Windows services for all the computers in their domain by using a combination of PowerShell and WMI.

As a systems administrator, one of the most common tasks is learning to manage Windows services, which are an important...

piece of Windows Server and clients.

The operating system and many applications depend on these services.

The familiar Windows Services Microsoft Management Console window lets administrators manipulate services on local or remote computers but doesn't scale too well to make changes on dozens or hundreds of machines at once.

This is where PowerShell comes in handy.

PowerShell has a few native cmdlets to manage Windows services.

Get-Command * Service* -Module Microsoft.PowerShell.Management

cmdlets for managing Windows services
There are several native cmdlets administrators can use to manage Windows services.

These cmdlets perform a lot of different actions on any service you'd like.

Get-Service is the most common cmdlet. At its most basic, Get-Service with no parameters shows all services on the local computer, but most administrators will need to find services on remote computers. This can be done with the ComputerName parameter.

Get-Service –ComputerName CLIENT1

Get-Service cmdlet
The Get-Service cmdlet without parameters displays the services running on a local machine.

Using Get-Service will detail all services that exist on the remote client.

How to handle several machines

What if you want to see the services on multiple computers? Perhaps you've got a list of computer names -- this list can be generated several ways: Active Directory, some line of business application, a client management system, a custom database -- in a text file. PowerShell has a cmdlet called Get-Content that can read each of these computer names; by using a foreach loop, we can run Get-Service for each of those computer names.

Get-Content C:\Computers.txt | foreach {Get-Service –ComputerName $_}

Changing a computer's service with PowerShell

Let's get a little deeper and change some of the services. A common practice is to restart a service if it's misbehaving or to apply a change. For example, let's restart the Application Layer Gateway (ALG) service on all computers in that text file. By specifying a particular service to find and then piping that object to Restart-Service, that's not a problem.

Get-Content C:\Computers.txt | foreach {Get-Service –ComputerName $_ -Name ALG | Restart-Service }

If the service is stopped, Restart-Service will start it.

Set-Service cmdlet further controls Windows services

Set-Service is another cmdlet that changes many different attributes on a Windows service, such as configuring the start mode on a service. It can define whether the service starts up when the operating system starts, is manually started or is disabled from starting at all. Set-Service has a StartupType parameter to change this attribute.

The commands below set the startup type for the ALG service to disabled on all computers in the C:\Computers.txt file.

Get-Content C:\Computers.txt | foreach {Get-Service –ComputerName $_ -Name ALG | Set-Service –StartupType Disabled }

Combine PowerShell and WMI for more functionality

There are some cases where the default service cmdlets either don't provide enough information or the ability to modify a certain attribute. For these instances, there is Windows Management Instrumentation (WMI). WMI has a class called Win32_Service that can be called using Get-CimInstance or Get-WmiObject.

Get-CimInstance –ComputerName client1 –ClassName Win32_Service

WMI Win32_Service
Using the WMI Win32_Service class with PowerShell divulges more details about Windows services on a computer.

Administrators can gather much more information about a service by using the Property parameter with an asterisk while querying the service via WMI.

Get-CimInstance –ComputerName CLIENT1 –ClassName Win32_Service –Property *

Property parameter in Win32_Service class
The Property parameter in the Win32_Service class can provide more detailed information about a Windows service.

Digging into the details

For a more advanced look at what the default service cmdlets are doing, you can look at the methods on the WMI service instance objects.

Get-WmiObject –ComputerName CLIENT1 –Class Win32_Service | Get-Member –MemberType Method

WMI service instance objects
Examining the methods on the WMI service instance objects.

These methods can be executed on the service objects by calling them.

Get-WmiObject –ComputerName CLIENT1 –Class Win32_Service –Filter "Name='ALG'" | foreach {$_.StopService()}

In the example above, I'm using the Filter parameter to select the ALG service only. I'm piping that to the alias to ForEach-Object and calling the StopService() method which, in turn, stops the service. Even though not necessary due to the Stop-Service cmdlet, this gives some insight of how the service cmdlets work.

Next Steps

Scripting storage provisioning in Windows Storage Space

The perks of PowerShell scripting for Windows Server administrators

These PowerShell cmdlets help admins manage remote servers

This was last published in March 2016

Dig Deeper on Windows PowerShell Scripting

PRO+

Content

Find more PRO+ content and other member only offers, here.

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

What are some effective ways you've used PowerShell or WMI for managing Windows services?
Cancel

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close