0

Get-service: Checking the Status of Windows Services with PowerShell

Services in Windows are one of the most important parts of the operating system. Previously, to get the status of a service on Windows, you had to use the services.msc graphical snap-in or the sc.exe command-line tool (for example, sc.exe query wuauserv). In this article,  learn that how to check the status of a service on Windows using PowerShell.

Use of Get-Service                                   

  • You can use the Get-Service PowerShell cmdlet to get a list of all services installed on the Window operatoing sytem.
  • Their status
  • Startup type

Cmdlets that are used to get the status and manage windows services, first time appeared in PowerShell 1.0.

In this article, we will know

  • How to get the status
  • Manage windows
  • Determine the dependency of services.

To get the complete syntax of the Get-Service cmdlet, run the command:

Get-help Get-Service

SYNTAX Get-Service [[-Name] <string[]>] [-ComputerName <string[]>] [-DependentServices] [-RequiredServices] [-Include <string[]>] [-Exclude <string[]>] [<CommonParameters>]

Use Get-Service to Check Windows Service Status

This command will list all local Windows services, their status (running or stopped), and display name:

Get-Service

Powershell get-service remote computer

The cmdlet returns a list of services sorted by name. The list contains the following Windows service properties:

Status — shows if the server is Running or Stopped;

Name — displays the short name of the service (used most often)

Display Name — full service name (more descriptive and human readable).

To save the services list to a text file for future investigation, use the Out-File cmdlet:

Get-Service | Out-File “C:\PS\Current_Services.txt”

To get two or more service states, you need to specify their names divided by commas:

Get-service bits, wuauserv

If you need to display only running services, use this command:

Get-Service | Where-Object {$_.Status -EQ “Running”}

The pipeline operator (|) passes the results to the Where-Object cmdlet, which selects only those services for which the Status parameter is set to “Running”. If you want to display only the stopped services, specify “Stopped”.

Powershell checks service status

You can get all the properties of the service object using the Get-Member.

Get-service | get-member

As you can see, these objects have the Typename — System.ServiceProcess.ServiceController. The screenshot shows all the available properties and methods of service objects in Windows (most of them are not used when displaying by default).

Powershell checks if service is running

You can display the properties of the specific service. For example, we need to display the Display Name, Status, and features of the Windows Update (wuauserv) service:

Get-service wuauserv | select Displayname,Status,ServiceName,Can*

DisplayName : Windows Update

Status: Stopped

CanPauseAndContinue : False

CanShutdown: False

stop: False

Powershell checks if service exists

find all services that can be paused and resumed without Windows restart:

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

For example, to get the type of Windows services startup type, run the command (works in PowerShell 5.1, introduced in Windows 10/Windows Server 2016):

Get-Service | select -property name,start type

Hint. You can check the PowerShell version on your computer with the command:

$PSVersionTable

If you only need to get the startup type of the service, run:

(Get-Service -Name wuauserv).StartupType

There are 4 possible types of starting services:

  1. Automatic — automatic start during Windows boot;

2. AutomaticDelayedStart — startup after system boot;

3. Manual — manual start;

4. Disabled — the service is disabled.

filter the services list by the service name using the asterisk as a wildcard:

Get-service wi*

Also, note that PowerShell is not a case-sensitive language. It means that the following commands will return equal results:

Get-service win*

Get-service WIN*

To exclude some services from the resulting list you can use the -Exclude option:

Get-Service -Name “win*” -Exclude “WinRM”

Powershell service status

You can sort services in descending order by the value of the Status property (running services are displayed earlier than stopped):

Get-service s* | sort-object status – Descending

How to Check if a Specific Service Exists via PowerShell?

If you want to check whether a specific Windows service currently exists, you can use the following commands (usually this check is used in various scripts):

$servicename = “SomeService”

If (Get-Service $servicename -ErrorAction SilentlyContinue)

{Write-Host “$servicename exists”

# do something}

Else {Write-Host ” $servicename not found”

# do something}

You can check if a specific Windows service exists on a list of remote computers/servers. To perform this task, you need to save the list of remote computers to the text file comp_list.txt in a simple format:

Server1

Server2

Server3

PC21

PC34

Powershell get service status

Now run the following PowerShell script:

$servicename = “SomeService”

$list = get-content “c:\ps\comp_list.txt”

Foreach ($server in $list) {

If (Get-Service $servicename -computername $server -ErrorAction ‘SilentlyContinue’){

Write-Host “$servicename exists on $server “

# do something

Else{write-host “No service $servicename found on $server.”}

Use PowerShell to Check Service Status on a Remote Computer

You can use the Get-Service cmdlet to get the status of services not only on the local but also on remote computers. To do this, use the –ComputerName parameter. You can use the NetBIOS, FQDN name, or an IP address as a computer name. Connection to remote computers is established not through PowerShell Remoting (WinRM), but Service Manager (similar to the sc.exe command).

Get-service wuauserv -ComputerName remotePC1

In PowerShell v3, you can get the status of the services on multiple remote computers at once (their names must be separated by commas).

Get-service spooler -ComputerName remotePC1,remotePC2, remotePC3| format-table Name,Status,Machinename –autosize

Get-service status

Use the format-table cmdlet in this example to get a more convenient table with the list of the status of services.

Also, you can get the health of the service on a list of remote computers that are being stored in a plain text file:

$list = get-content “c:\ps\comp_list.txt”

Get-Service -Computername (Get-Content -path “c:\ps\comp_list.txt”) -Name spooler | Select-Object MachineName,Name,Displayname,Status | Sort-Object Status

Or you can check the service state on all computers in a specific AD OU:

$Computers = (Get-ADComputer -SearchBase ‘OU=Servers,OU=UK,DC=solutionviews,DC=com’).names

Foreach ($computer in $computers)

{Get-Service -ComputerName $computer.Name -Name wuauserv | Where-Object {$_.status -eq “running”} | select status,name,machinename}

To restart a service on a remote computer, use the following command:

Get-Service wuauserv -ComputerName server1| Restart-Service

Use Get-Service to Display Service Dependencies

The Get-Service cmdlet has two other useful parameters you can use when managing Windows services. The DependentServices parameter returns services that depend on this service. The RequiredServices parameter returns the services on which this service depends.

The following command receives the services that needed to be run before starting the LanmanWorkstation service.

Get-Service -Name LanmanWorkstation –RequiredServices

Powershell list services

The next command returns dependent services that require the LanmanWorkstation service.

Get-Service -Name LanmanWorkstation -DependentServices

If you manually stop all dependence services, you won’t be able to run your service. In order to automatically run all dependency services, use the following PowerShell one-liner:

Get-service servicename | Foreach {start-service $_.name -passthru; start-service $_.DependentServices -passthru}

Managing Windows Services via PowerShell

There are several other useful cmdlets in the PowerShell module for managing services. To list all available PowerShell commands used to manage Windows services, run:

Get-Command -Noun Service

Get-service powershell

You can use these cmdlets to start, stop, restart, or change the startup type of the service.

To manage services, be sure to run powershell.exe as an administrator. You must start, stop, restart, or change the startup type of the service.

Stop service:

Stop-Service -Name Windefend

When you stop some services, an error may appear:

Stop-Service: Cannot stop service ‘SQL Server (MSSQLSERVER) (MSSQLSERVER)’ because it has dependent services. It can only be stopped if the Force flag is set.

To force stop this and the dependent service, use the -Force parameter:

Stop-Service –Name MSSQLSERVER -Force

Start the service:

Start-Service -Name Windefend

Restart the service:

Restart-Service -Name Windefend

If you need to change the startup type of the service,

for example from Automatic to Disabled, run:

Set-Service ‘WinRM’ -StartupType Disabled

Donate! If you like this Article and saved your Day...
BTC Addres:
  • Send only BTC to this deposit address.
  • Ensure the network is BNB Smart Chain (BEP20).

Leave a Reply

Your email address will not be published. Required fields are marked *




Enter Captcha Here :