PowerShell is a great scripting tool. It not only helps save you time, it also provides greater flexibility to execute repeated tasks manually or via scheduled tasks. Almost all Windows roles and features ship with PowerShell cmdlets.
You can use Windows Operating System cmdlets, available by default, to perform operational tasks such as checking the status of a particular Windows service on multiple computers, checking the list of software installed on Windows computers, checking and collecting roles and features installed on Windows Server Operating Systems and much more.
One of the tasks Windows administrators frequently perform is checking the status of critical services on critical production servers such as Exchange Servers, SQL Servers and/or Active Directory domain controllers.
Though you can use SCCM and similar tools to provide you with a report on the status of services from a particular set of machines, SCCM requires that the SCCM Client is working on all target computers before SCCM can report on the status of services.
This is where our latest script and Server Tutorial comes in handy. You can use the PowerShell script explained in this article to quickly and easily check the status of particular services on a single or multiple Windows computers.
Checking the Status of a Single Service
If you would like to check status of a particular service on a remote computer, what you can do is execute the PowerShell command below:
Get-WMIObject -Computer -Query "Select * From Win32_Service WHERE Name Like "%SomeServiceName%"
Executing the above PowerShell command will help you get the status of a specific service specified in the “%SomeServiceName% parameter. This command connects to a remote computer specified in the “-Computer” parameter. You’ll likely want to change both the computer and service names before executing the above command.
Checking the Status of Multiple Services
In case you would like to get a list of all Windows Services that are running on a target system, execute the following PowerShell command:
Get-WMIObject Win32_Service -Computer | Where {$_.State -eq "Running"} | FT -Auto
The above command will list all Windows Services that are running from the remote computer specified in the “-Computer” parameter, and the output will be displayed in the PowerShell window. If you would like to save the output in a CSV file, adding “Export-CSV” will do the job as shown in this command:
Get-WMIObject Win32_Service -Computer | Where {$_.State -eq "Running"} | FT -Auto | Export-CSV "C:TempServicesStatus.CSV"
Checking the Status of a Single Service on Multiple Computers
While the above PowerShell commands will help you get you the status of a specific or multiple services from a single Windows computer, the following PowerShell script can be used if you would like to see the status of a single service from multiple Windows computers. All you need to do is create a text file that contains the Windows computer names and modify the script variable “$ServiceNameToCheck” to include the service you would like to check.
$ServiceNameToCheck ="SomeServiceName"
$ReportFile = "C:TempServiceStatus.CSV"
Remove-item $ReportFile -ErrorAction SilentlyContinue
$ThisSTR = "Computer Name, Service Status"
Add-Content $ReportFile $ThisSTR
$CompFile = "C:TempComputers.TXT"
Foreach ($ComputerName in Get-Content "$CompFile")
{
$Error.Clear()
Get-WMIObject -Computer $ComputerName -Query "Select * From Win32_Service WHERE Name Like "%$ServiceNameToCheck% AND {$_.State -eq "Running"}
IF ($Error.Count -ne 0)
{
$ThisSTR = $ComputerName+", Could Not Connect to Remote Computer or Service Not Running"
Add-Content $ReportFile $ThisSTR
}
else
{
$ThisSTR = $ComputerName+", Running"
Add-Content $ReportFile $ThisSTR
}
}
Write-Host "Service status was retrieved from the list of computers and the output was saved in $ReportFile"
Once the PowerShell script above has finished executing, a report file will be created named ServiceStatus.CSV in the “C:Temp” folder that contains the status of the service name specified in the “$ServiceNameToCheck” variable from all computers mentioned in the “C:TempComputers.TXT” file.
Note that the script checks to make sure the command was executed successfully before it reports the status of the service. The PowerShell script uses the “Get-WMIObject” PowerShell cmdlet to collect the status of specific service(s) from target computers. In case the Get-WMIObject is not able to connect to a target computer or if the service status is not retrieved, it will return a “Could Not Connect to Remote Computer or Service Not Running” message in the report.
Conclusion
It is quite easy to get the status of a single or multiple services from a single or multiple remote Windows computers. While there are enterprise tools available to collect an inventory from Windows computers, including the status of services, there are also cases where these PowerShell commands/scripts come in very handy, particularly when you need to quickly check the status of a particular service on multiple Windows computers.
Nirmal Sharma is a MCSEx3, MCITP and Microsoft MVP in Directory Services. He specializes in directory services, Microsoft Azure, Failover clusters, Hyper-V, System Center and Exchange Servers, and has been involved with Microsoft technologies since 1994. In his spare time, he likes to help others and share some of his knowledge by writing tips and articles on various sites and contributing to Health Packs for ADHealthProf.ITDynamicPacks.Net solutions. Nirmal can be reached at nirmal_sharma@mvps.org.