Get Windows Service Status By using Power Shell Script

How to Get Windows Service status in windows server using simple PowerShell script?

To check windows service for a server, we need to use service.msc in command prompt and go to particular service to find the status.

It’s better to use Powershell function, instead of using this service.msc. We can get service status remotely by Powershell.

Function : Get-WMIObject

Get Windows Service function will not only use by Windows Admins but also SQL Server DBA will also use this PowerShell function.

Whenever windows servers reboot as part of a Monthly maintenance activity where SQL Server installed. DBAs also need to check SQL Server services in all the rebooted servers.

Syntax:

Get-WMIObject Win32_Service -computername Server1| Where-Object {$_.name -Match "AppIDSvc"} | Select-Object SystemName,displayname,Name, startmode,status,state| ft

Above code will display ServerName, Service Display name, Service Name, Start mode, Status and State. Please check the output below

OutPut:

System      Name displayname          Name             startmode      status      state
----------  -----------               ----             ---------      ------      -----
Server1     Application Identity      AppIDSvc          Manual        OK      Stopped

Above code will help you get the services information about the server which has AppIDSvc in the name.

If you want to get info about 10 servers. No need to connect each and every server, It can be automated by using Powershell.

We need to give all the server names in notepad and save it as below image

Input:

Get Windows Service

We need to give a list of the server as Input which we need to get info

example:

server1
server2
server3

Get Windows Service

Please use the below code to automate.

CODE:

$Servers = Get-Content "C:\PowerShell\Servicecheck\serversList.txt"
@(
foreach ($Server in $Servers)
{
Get-WMIObject Win32_Service -computername $Server| Where-Object {$_.name -Match "MSSQL"} | Select-Object SystemName,displayname, Name , startmode , status , state| ft
Write-host "$Server completed"
}
)|Out-file -FilePath "C:\PowerShell\Servicecheck\output.txt"

Output:

Get Windows Service

Note: To learn more about Get-WMIOject function, Please click below link.
Click Me

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *