How to check a Windows Service status with PowerShell

I am a developer from Nebraska in the US.
Search for a command to run...

I am a developer from Nebraska in the US.
Really informal.
When you are in a flow, nothing is worse than having to stop to deal with a console error message. It would be great if the errors could be handled by the script and packaged into helpful messages. Happily, PowerShell offers this capability to catch,...

If you have ever forked a repository on github, you have likely encountered the terms merge and rebase when researching how to interact with the original repository. Merging and rebasing are two different methods to solve the same problem, namely, ho...

If you use a personal access token to authenticate to github, and you should be using one, you may have encountered the below error when trying to push updates to a remote repository. (refusing to allow a Personal Access Token to create or update wor...

This short guide is intended to flatten the git learning curve for a beginner. The guide will cover the basics of getting started with git on the command line. Cloning a Repository Cloning a repository is the git term for downloading a remote project...

If you have ever installed a new library or language on your mac, you have probably experienced the frustration of receiving “Command Not Found” when trying to use it in the terminal. This is especially challenging for beginners who may wonder what h...

PowerShell offers simple ways to perform administrative tasks such as checking the status of a service. This can be combined with an if statement to act on a stopped service automatically. These commands can be incorporated into a much larger script to check multiple services.
The first step is to store the service name in the variable $ServiceName. Then you can use another variable to store the service information which contains the status information. The Get-Service command can be used to retrieve this information.
Then, using an if statement, you can compare the service info status to a static value. In the example, the service info status is checked against the value Running which determines whether is started and running. The actions can then be taken on the service such as starting it or alerting the appropriate parties. In the example, the script attempts to start the service, then performs a refresh of the service information and writes the new status to the console log.
$ServiceName = ‘Service’
$ServiceInfo = Get-Service -Name $ServiceName
if ($ServiceInfo.Status -ne ‘Running’) {
write-host ‘service is not started, starting service’
Start-Service -Name $ServiceName -verbose
$ServiceInfo.Refresh()
write-host $ServiceInfo.Status
}
write-host ‘all done’