Powershell

How to Manage Windows Services with PowerShell

By October 21, 2020No Comments

You can manage Windows services not only from the services.msc snap-in or sc.exe command line tool, but also using PowerShell. In this post we’ll consider different scenarios of managing Windows services with PowerShell.

PowerShell Cmdlets Used to Manage Windows Services

There are eight basic Service cmdlets to view the state of Windows services and manage them. To get the full list of service management cmdlets, run this command:

Get-Help \*-Service

basic powershell cmdlets to manage windows services
  • Get-Service — allows to get the services on a local or remote computer both in running or stopped state;
  • New-Service – creates a service. The cmdlet creates a new entry for a Windows service in the registry and in the service database;
  • Restart-Service – restarts a service. The cmdlet sends the restart message through the Windows Service Controller;
  • Resume-Service – resumes a service. The cmdlet sends a resume message to Windows Service Manager;
  • Set-Service — changes the settings of a local or remote service, including its state, description, displayed name or startup mode. You can also use this cmdlet to start, stop or suspend a service;
  • Start-Service – starts a service;
  • Stop-Service – stops a service (the cmdlet sends a stopping message to Windows Service Manager);
  • Suspend-Service – suspends a service. A suspended service is still running, but it does not do anything till it is resumed using( for example, with the Resume-Service cmdlet).

You can get a detailed description and examples of using a particular cmdlet with Get-Help:

Get-Help Start-Service

Get-Help

How to Check Windows Service Status with Get-Service?

You can get the list of services and their state (Running/Stopped) on a local or remote computer using the Get-Service cmdlet. The –Name parameter allows to select services by name. The service name can be specified using the wildcard character *.

Check Windows Service Status with Get-Service

If you do not know the exact service name, you can find it by its displayed name using the –DisplayName parameter. You can use the list of values and wildcards.

powershell get services

Use the Get-Service cmdlet with the -ComputerName parameter to get the service status on a remote computer. You can query the service status on multiple remote computers at once by specifying their names separated by commas. For example, the command shown below gets the Spooler service status on the remote computers ny-prnt1 and ny-prnt2.

Get-Service spooler –ComputerName ny-prnt1,ny-prnt2

Status Name DisplayName
------ ---- -----------
Running spooler Print Spooler
Stopped spooler Print Spooler

To display all the properties of a service, use the Select-Object cmdlet:

Get-Service spooler | Select-Object *

Get-Service properties

The Select-Object cmdlet allows to get specific properties of a service. For example, you want to view the name, status and available options of the Spooler service:

Get-Service Spooler | Select DisplayName,Status,ServiceName,Can*

Get-Service state

The Get-Service cmdlet has two parameters that allow you to view the service dependencies:

  • -DependentServices allows to display the services dependent on the given service
  • -RequiredServices displays the services the given service depends on

The following command displays the services required to start the Spooler service:

Get-Service –Name Spooler -RequiredServices

Get-Service RequiredServices

The following command shows the services that depend on Spooler:

Get-Service –Name Spooler -DependentServices

Get-Service DependentServices

If you want to find the services with the specific state or parameters, use the Where-Object cmdlet. For example, let’s get the list of running services:

Get-Service | Where-Object {$_.status -eq 'running'}

Get-Service: list of running services

To display the services with the manual startup type, run this command:

Get-Service | Where-Object {$_.starttype -eq 'Manual'}

get service with the manual startup type

Use the following PowerShell script to check if a specific Windows service exists on a current computer:

if (Get-Service "ServiceTest" -ErrorAction SilentlyContinue)
{
Write-host "ServiceTest exists"
}

How to Stop, Start, or Restart Service with PowerShell?

You can stop a service using the Stop-Service cmdlet. To stop the Spooler, run the command:

Stop-Service -Name spooler

The Stop-Service cmdlet does not display anything after execution. To see the result, use the -PassThru parameter.

Stop-Service PassThru

Please note that not every service can be stopped. If there are any dependent services, you will see an error:

Cannot stop service because it has dependent services. It can only be stopped if force flag set.
Cannot stop service because it has dependent services. It can only be stopped if force flag set.

To force a service to stop, use the –Force parameter. You should remember that all dependent services will also stop:

Stop-Service samss –Force -Passthru

The following command will stop the specified services (bits, spooler) if they are in “Running” state:

get-service bits,spooler | where {$_.status -eq 'running'} | stop-service –passthru

stop services in the running state

Sometimes services hang up in the “Stopping” state and have to be killed forcibly.

The Start-Service cmdlet starts a stopped service:

Start-Service -Name spooler -PassThru

Start-Service

A service won’t start, if any of its dependent services is stopped. To find and start them use the following PowerShell one-liner:

get-service samss | Foreach { start-service $_.name -passthru; start-service $_.DependentServices -passthru}

powershell start a service with all dependences

The Suspend-Servce cmdlet can pause services if they support this state. To learn if a service can be suspended, use the Get-Service cmdlet with the CanPauseAndContinue property.

Get-Service samss | Format-List name, canpauseandcontinue

Get-Service canpauseandcontinue

To display the list of all services that may be paused, run this command:

Get-Service | Where-Object {$_.canpauseandcontinue -eq "True"}

get service with the canpauseandcontinue option

Let’s suspend SQLBrowser service:

Suspend-Service -Name SQLBrowser

Suspend-Service

To resume a suspended service, use the Resume-Service cmdlet:

Resume-Service -Name SQLBrowser

Resume-Service

The following command will resume all suspended services:

get-service | where-object {$_.Status -eq "Paused"} | resume-service

The Restart-Service cmdlet will restart a service:

Restart-Service -Name spooler

Restart-Service

This command starts all stopped network services on a computer:

get-service net* | where-object {$_.Status -eq "Stopped"} | restart-service

These commands do not have the –ComputerName parameter, but you can run them on a remote computer using the Invoke-Command cmdlet or a pipe.

For example, to restart a print spooler on the remote computer ny-prnt1, run the command:
Get-Service Spooler -ComputerName ny-prnt1 | Start-ServiceBy default, only administrators may start/stop Windows services, but you can grant non-admin users start/stop/restart permissions on a specific service.

Using Set-Service to Change Service Settings

The Set-Service cmdlet allows you to change any parameters or settings of a service on a local or remote computer. Since the state of a service is a property, you can use this cmdlet to start, stop or suspend a service. Set-Service has the -StartupType parameter that allows to change the startup type of a service.

Let’s change the Spooler startup type to automatic:

Set-Service spooler –startuptype automatic –passthru

change the startup type of the Print Spooler to Automatic

You can set the manual startup type:

Set-Service spooler –startuptype manual –passthru

set the manual startup type Set-Service

How to Create or Delete a Windows Service via PowerShell?

New-Service – is a cmdlet to create a new service in Windows. Specify the name and the executable file for the new service (you can even run a PowerShell script as a Windows service).

Let’s create a new service with the name TestSvc:

new-service -name TestSvc -binaryPathName "C:\WINDOWS\System32\svchost.exe -k netsvcs"

new-service create service with powershell

Get the information about the startup type and description of the service using the Get-WmiObject cmdlet.

get-wmiobject win32_service -filter "name='testservice'"

get-wmiobject win32_service

You can change the settings of the new service using the following command:

Set-Service -Name TestSvc -Description ‘My Service’ -StartupType Manual

change service startup type to manual

To delete a service, run this command:

(Get-WmiObject win32_service -Filter ″name=′TestSvc′″).delete()

Change the User Account that Runs the Windows Service

You can use PowerShell in order to change user account used to start a service. Get the name of the account used to start TestSvc:

get-wmiobject win32_service -filter "name='TestSvc'" | Select name,startname

get the user account to run the service

To change the username and password for a Windows Service, run the following commands:

$svc = get-wmiobject win32_service -filter "name='TestSvc'"
$svc.GetMethodParameters("change")

The list of the Change() method parameters is displayed. Count where the StartName and StartPassword parameters are: they located on the 20th and 21st places respectively.

change runas user account of a windows service

$svc | Invoke-WmiMethod -Name Change –ArgumentList @ ($null,$null,$null,$null,$null,$null,$null, $null,$null,$null,$null,$null,$null,$null,$null,$null, $null,$null,$null,"Administrator","!123Pa$$w0rd")

Or you can enter the name of a gMSA account (the account password is not specified in this case).

As you can see, PowerShell makes it easy to manage Windows services. You can create, stop, start or resume services, and change their properties. Most cmdlets allow to manage services on remote computers.

Leave a Reply