Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
11.5k views
in PowerShell by 8 8 10

I have a Windows PowerShell script that mainly depends on a particular service to be running! so How to check if a service is started or not using PowerShell?

P.S: I need to check the status of  "World Wide Web Publishing Service" in PowerShell.

check if service is running powershell


1 Answer

1 like 0 dislike
by 159 200 385
selected by
 
Best answer

To check if a service started or not using PowerShell, you have to get the service object using Get-Service, then get its status using the Status property as below:

$ServiceName = "World Wide Web Publishing Service"
$ServiceStatus = (Get-Service -Name $ServiceName).status
if($ServiceStatus -eq "Running")
{
   Write-Host "Running"
}
else {
   Write-Host "Stopped"
}

Output

how to check if service is started in powershell

The Get-Service cmdlet gets objects that represent the services on a computer, including running and stopped services. Read more at Get-Service

by 8 8 10
0 0
Very helpful, thank you!
If you don’t ask, the answer is always NO!
...