PowerShell : Services

 

There are nine core Service cmdlets, designed for a wide range of service tasks.

  • Get-Service
  • New-Service
  • Restart-Service
  • Resume-Service
  • Set-Service
  • Start-Service
  • Stop-Service
  • Suspend-Service
  • Remove-Service

you can get the above by using Get-Help \*-Service, and you can find information about each Service cmdlet by using Get-Help .

Get-Service

The Get-Service cmdlet gets services on a computer, including running and stopped services. This cmdlet displays the status, service name, and display name of each service.

Get all the services on a computer

PS C:\> Get-Service
 - or -
PS C:\> Get-Service *

Status   Name               DisplayName
------   ----               -----------
Stopped  AdobeARMservice    Adobe Acrobat Update Service
Stopped  AJRouter           AllJoyn Router Service
Stopped  ALG                Application Layer Gateway Service
Stopped  AppIDSvc           Application Identity
............................
..................................

Get services that begin with “wmi”

PS C:\> Get-Service "wmi*"
 - or -
PS C:\> "wmi*" | Get-Service

Status   Name               DisplayName
------   ----               -----------
Stopped  wmiApSrv           WMI Performance Adapter

Display services which contain “network”

PS C:\> Get-Service -Displayname "*network*"

Status   Name               DisplayName
------   ----               -----------
Stopped  NcaSvc             Network Connectivity Assistant
Running  NcbService         Network Connection Broker
Running  NcdAutoSetup       Network Connected Devices Auto-Setup
...................
..................................

Get services that begin with win and an exclude WinRM

PS C:\> Get-Service -Name "win*" -Exclude "WinRM"

Status  Name               DisplayName
------  ----               -----------
Running WinDefend Windows  Defender Antivirus Service
Running WinHttpAutoProx... WinHTTP Web Proxy Auto-Discovery Se...
Running Winmgmt Windows    Management Instrumentation

Display services that are currently running

PS C:\> Get-Service | Where-Object {$_.Status -eq "Running"}

Status   Name               DisplayName
------   ----               -----------
Running  WinDefend          Windows Defender Antivirus Service
Running  WinHttpAutoProx... WinHTTP Web Proxy Auto-Discovery Se...
Running  Winmgmt            Windows Management Instrumentation

Sort services by property value

PS C:\> Get-Service "s*" | Sort-Object status

Status   Name               DisplayName
------   ----               -----------
Stopped  svsvc              Spot Verifier
Stopped  SharedAccess       Internet Connection Sharing (ICS)
Stopped  shpamsvc           Shared PC Account Manager
Stopped  SensorService      Sensor Service
Stopped  SensrSvc           Sensor Monitoring Service
Stopped  SessionEnv         Remote Desktop Configuration
Stopped  spectrum           Windows Perception Service
.....................
.............................

Get the dependent services of a service

PS C:\> Get-Service "WinRM" -RequiredServices

Status   Name               DisplayName
------   ----               -----------
Running  RPCSS              Remote Procedure Call (RPC)
Running  HTTP               HTTP Service

Start-Service

The Get-Service cmdlet starts one or more stopped services. It sends a start message to the Windows Service Controller for each of the specified services.

If a service is already running, the message is ignored without error.

Start a service by using its name

PS C:\> Start-Service -Name workfolderssvc

Display information without starting a service

PS C:\> Start-Service -DisplayName "*Smart Card*" -WhatIf
What if: Performing the operation "Start-Service" on target "Smart Card (SCardSvr)".
What if: Performing the operation "Start-Service" on target "Smart Card Device Enumeration Service (ScDeviceEnum)".
What if: Performing the operation "Start-Service" on target "Smart Card Removal Policy (SCPolicySvc)".

Here, -DisplayName parameter is used to specify the services and -WhatIf displays what would occur if you run the command without making changes.

Start a disabled service

Before starting the service we have to change the start type of the service to “Manual”.

PS C:\> Set-Service SCardSvr -StartupType manual
PS C:\> Start-Service SCardSvr

Stop-Service

The Get-Service cmdlet stops one or more running services. It sends a stop message to the Windows Service Controller for each of the specified services.

Start a service by using its name

PS C:\> Stop-Service -Name "workfolderssvc"

Stop a service by using the display name

PS C:\> Service -DisplayName "*Smart Card*" | Stop-Service

Stop a service which has dependent services

PS C:\> Get-Service -Name "WinRM" | Format-List -Property Name, DependentServices
PS C:\> Stop-Service -Name "WinRM" -Force -Confirm

Suspend-Service

The Get-Service cmdlet suspends (pauses) one or more running services. It sends a suspend message to the Windows Service Controller for each of the specified services.

Suspend a service

PS C:\> Suspend-Service -Name "workfolderssvc"

Display what would happen if you suspend services

PS C:\> Suspend-Service -Name "workfolderssvc" -WhatIf

Suspend all services that can be suspended

PS C:\> Get-Service | `
Where-Object {$_.CanPauseAndContinue -eq "True"} | `
Suspend-Service -Confirm

backtick (`) is the same as \ in Unix to continue a new line.

Resume-Service

The Get-Service cmdlet resumes one or more suspended (paused) services. It sends a resume message to the Windows Service Controller for each of the specified services.

Resume a service

PS C:\> Resume-Service -Name "workfolderssvc"

Resume all suspended services

PS C:\> Get-Service | `
Where-Object {$_.Status -eq "Paused"} | `
Resume-Service

Restart-Service

The Get-Service cmdlet restarts one or more services. It sends a stop message and then a start message to the Windows Service Controller for each of the specified services.

Restart a service

PS C:\> Restart-Service -Name winmgmt

Start all stopped network services

PS C:\> Get-Service -Name "net*" | `
Where-Object {$_.Status -eq "Stopped"} | `
Restart-Service

New-Service

The Get-Service cmdlet creates a new Windows service. It creates a new entry in the registry and in the service database.

A new service requires an executable file that runs during the service.

The parameters of this cmdlet let you set the display name, description, startup type, and dependencies of the service.

Create a service

PS C:\> New-Service -Name "TestService" `
-BinaryPathName "C:\WINDOWS\System32\svchost.exe -k netsvcs"

Create a service that includes a description, startup type, and display name

PS C:\> New-Service -Name "TestService" `
-BinaryPathName "C:\WINDOWS\System32\svchost.exe -k netsvcs" `
-DependsOn NetLogon `
-DisplayName "Test Service" `
-StartupType Manual `
-Description "This is a test service."

View the new service

PS C:\> Get-WmiObject win32_service -Filter "name='testservice'"
ExitCode : 0
Name : testservice
ProcessId : 0
StartMode : Auto
State : Stopped
Status : OK

Remove-Service

The Get-Service cmdlet removes a Windows service. It removes a Windows service in the registry and in the service database.

Remove a service

PS C:\> Remove-Service -Name "TestService"

Remove a service using the display name

PS C:\> Get-Service -DisplayName "Test Service" | Remove-Service