Quantcast
Viewing all articles
Browse latest Browse all 91

Get-service: Checking the Status of Windows Services with PowerShell

Image may be NSFW.
Clik here to view.
get service command

You can use the Get-Service cmdlet to get a list of all the services installed on the Windows operating systems, their status and startup type. This one and other cmdlets to get the status and management of Windows services first time appeared in Powershell 1.0. In this article we will demonstrate typical examples of Get-Service cmdlet usage to get the status of a service on local or remote computers, the type of services startup, also we’ll cover how to determine the dependencies of services.

You can get a list of services on a local or remote computer by using the Get-Service cmdlet. Get-Service command without parameters returns a list of all services on the local system.

Use Get-Service to Check Windows Service Status

This command will list all local Windows services, their status (running or stopped) and display name.

Image may be NSFW.
Clik here to view.
get-service remote computer

To save the service list to a text file for future investigation, use the Out-File cmdlet:

Get-Service | Out-File "C:\PS\Current_Services.txt"

To get two or more service state, you need to specify its name divided by commas:

get-service bits, wuauserv

If you need to display only running services, use this command:

Get-Service | Where-Object {$_.Status -EQ "Running"}

The pipeline operator (|) passes the results to the Where-Object cmdlet, which selects only those services for which the Status parameter is set to “Running”. If you want to display only the stopped services, specify “Stopped”.

Image may be NSFW.
Clik here to view.
powershell get service status

You can get all the properties of the service object using the Get-Member.

get-service | get-member

As you can see, these objects have the Typename –  System.ServiceProcess.ServiceController. The screenshot shows all the available properties and methods of service objects in the system (most of them are not used when displaying by default).

Image may be NSFW.
Clik here to view.
powershell get-service remote computer

To display specific properties of the service, use the next command. For example, we need to display the Display Name, status and features of the Windows Update (wuauserv) service:

get-service wuauserv | select Displayname,Status,ServiceName,Can*

DisplayName : Windows Update

Status : Stopped

CanPauseAndContinue : False

CanShutdown : False

CanStop : False

Image may be NSFW.
Clik here to view.
powershell check service status

You can find all services that can be paused and resumed without Windows restart:

Get-Service | Where-Object {$_.canpauseandcontinue -eq "True"}

For example, to get the type of Windows services startup type, run the command (works in PowerShell 5.1):

Get-Service | select -property name,starttype

You can filter the services list by the service name using the asterisk as a wildcard:

get-service wi*

Also, note that the PowerShell is not a not case-sensitive language. It means that the following command will return the equal results:

get-service win*

get-service WIN*

To exclude some service from resulting list you can use the -Exclude option:

Get-Service -Name "win*" -Exclude "WinRM"

You can sort services in descending order by the value of the Status property (running services are displayed earlier than stopped):

get-service s* | sort-object status – Descending

How to Check if a Specific Service Exists via PowerShell?

If you want to check whether a specific Windows service in exist on current, you can use the following commands (usually used in various scripts):

$servicename = “SomeService”

if (Get-Service $servicename -ErrorAction SilentlyContinue)

{

Write-Host "$servicename exists"

# do something

}

Else {

Write-Host " $servicename not found"

# do something

}

Image may be NSFW.
Clik here to view.
get service status powershell

You can check if a specific Windows service exists on a list of remote computers/servers. To do this task you need to save the list of remote computers to the text file comp_list.txt in the simple format:

server1

server2

server3

PC21

PC34

Image may be NSFW.
Clik here to view.
powershell get-service

And run the following PowerShell script:

$servicename = "SomeService"

$list = get-content “c:\ps\comp_list.txt”

foreach ($server in $list) {

if (Get-Service $servicename -computername $server -ErrorAction 'SilentlyContinue'){

Write-Host "$servicename exists on $server "

# do something

}

else{write-host "No service $servicename found on $server."}

}

Use PowerShell to Check Service Status on a Remote Computer

You can use the Get-Service cmdlet to get the status of services not only on the local, but also on remote computers. To do this, use the –ComputerName parameter. You can use the NetBIOS, FQDN name or an IP address as a computer name. Connection to remote computers is established not through PowerShell Remoting (WinRM), but through Service Manager (similar to the sc.exe command).

get-service wuauserv -ComputerName remotePC1

In PowerShell v3 you can get the status of the service on multiple remote computers at once, their names must be separated by commas.

get-service spooler -ComputerName remotePC1,remotePC2, remotePC3| format-table Name,Status,Machinename –autosize

Image may be NSFW.
Clik here to view.
powershell service status

Use the format-table cmdlet in this example to get a more convenient table with the list of the services status.

Also, you can get the services health on a list of remote computers that is being stored in a plain text file:

$list = get-content “c:\ps\comp_list.txt”
Get-Service -Computername (Get-Content -path “c:\ps\comp_list.txt”) -Name spooler | Select-Object MachineName,Name,Displayname,Status | Sort-Object Status

To restart a service on a remote computer, use the following command:

Get-Service wuauserv -ComputerName server1| Start-Service

Use Get-Service to Display Service Dependencies

The Get-Service cmdlet has two other useful parameters that you can use when managing Windows services. The DependentServices parameter returns services that depend on this service. The RequiredServices parameter returns the services on which this service depends.

The following command receives the services needed to start the LanmanWorkstation service.

Get-Service -Name LanmanWorkstation –RequiredServices

Image may be NSFW.
Clik here to view.
get-service powershell

The next command returns dependent services that require the LanmanWorkstation service.

Get-Service -Name LanmanWorkstation -DependentServices

If you manually stop all dependence services, you won’t be able to run your service. In order to automatically run all dependency services, use the following PowerShell one-liner:

get-service servicename | Foreach { start-service $_.name -passthru; start-service $_.DependentServices -passthru}

The post Get-service: Checking the Status of Windows Services with PowerShell appeared first on TheITBros.


Viewing all articles
Browse latest Browse all 91

Trending Articles