How to check whether a specific service exists using Powershell?

2

1

Introduction

According to this documentation it is possible to check which services have been stopped on Windows by executing the following command:

Get-Service | Where-Object {$_.status -eq "stopped"}

in PowerShell.

Question

Which command needs to be issued in PowerShell in order to check whether a certain service, e.g. tomcat8 exists?

030

Posted 2014-06-16T21:55:53.280

Reputation: 1 924

Answers

9

You can specify the service name using the -Name attribute. By default if it doesn't see a matching service it will give an error. Using -ErrorAction SilentlyContinue you can get an empty variable back.

$service = Get-Service -Name W32Time -ErrorAction SilentlyContinue

Once you have that you can just see if the length is greater than 0.

if ($service.Length -gt 0) {
    # Do cool stuff
    }

Tim Ferrill

Posted 2014-06-16T21:55:53.280

Reputation: 588

Snippets from the answer have been combined in a ps1 file and it works. Thank you. – 030 – 2014-06-16T22:46:35.177