I recently compiled a script to check a service based on a parameter to check is a service is running and if not start, the parameter value is required to be specified as the service name;
# Gets the service specified in the command line arguement
$Service = Get-Service | Where-Object {$_.Name -eq $args[0]}
# Checks if the service status is stopped and starts the service
If ($Service.Status -eq “Stopped”)
{
Start-Service -Name $Service.Name
}
The above is saved as a powershell script (Start-StoppedService.ps1) and can be invoked from a command line specifying the parameter as below, where the Spooler service is checked;
Start-StoppedService Spooler