Table of Contents
Using Get-Service
Cmdlet
Use Get-Service
with the Where-Object
cmdlet to check if a particular service is running on our local machine.
1 2 3 4 5 6 7 |
Get-Service | Where-Object { ($_.Status -EQ "Running") -and ($_.DisplayName -EQ "Workstation") } |
1 2 3 4 5 |
Status Name DisplayName ------ ---- ----------- Running LanmanWorkstation Workstation |
If you need to put if condition, then head over to this section .
We used the Get-Service
cmdlet to get a list of all services on a local computer; the list is then piped to the Where-Object
cmdlet, which filters the list based on the specified criteria. The script block is enclosed in curly braces ({}
) and contains a script that is run for each service in the list.
The script block in this command contained two conditions that checked the status of each service ($_.Status
) and the display name of each service ($_.DisplayName
). The condition was a Boolean expression that was True
only if the status of the service was Running
(-EQ "Running"
) and the display name of the service was Workstation
(-EQ "Workstation"
). The -and
operator combined the two conditions and returned True
if both were true.
The Get-Service
cmdlet is only available on the Windows platform, used to retrieve all the services on our local or remote computer. First, it gets the objects represented as services on the computer, which include Stopped
and Running
services. Then, it displays the service-related information, for instance, the Status
, DisplayName
, and Name
of the service.
By default, when running the Get-Service
cmdlet without parameters, all the local computer’s services are returned. We can direct this cmdlet to retrieve only particular services by giving the service name or the display name of the services, or we can pipe service objects to this cmdlet.
The resulting list includes only the services that meet both conditions. If there are no services that meet both conditions, the list will be empty.
Use Get-Service
Cmdlet with if-else
Block
To check if a specific service is running in PowerShell:
- Get a service object of a service name
WSearch
and save it in a variable. - Use the
if-else
block to assess a service object’sLength
property (here, we used the service object created in the previous step). - Print
Running
if the value of the.Length
property is greater than0
; otherwise,Stopped
.
1 2 3 4 5 6 7 8 9 |
$service = Get-Service -Name WSearch -ErrorAction SilentlyContinue if ($service.Length -gt 0) { Write-Host "Running" }else{ Write-Host "Stopped" } |
This PowerShell script retrieved a service object for the service with the name WSearch
using Get-Service
cmdlet and checked the .Length
property of the service object. If it is greater than 0
, then the service is running, and the script displays a message saying Running
; otherwise, the service is stopped, and the message would be saying Stopped
.
Note that this script will not work if the service is paused because the
$service
object will still exist even if the service isPaused
. To check for a paused service, you will need to use the.Status
property of the service object like this:
1 2 3 4 5 6 7 8 9 10 11 |
$service = Get-Service -Name WSearch -ErrorAction SilentlyContinue if ($service.Status -eq "Running") { Write-Host "Running" } elseif ($service.Status -eq "Paused") { Write-Host "Paused" } else { Write-Host "Stopped" } |
The above script will correctly display a message depending on the status of the service, whether it is running, paused, or stopped.
Until this point, we have learned how to check the status of a service, whether it is running, stopped or paused. What if we have a situation to create a service application, run it and check its current status? Let’s learn that in the following section, but before that, it is essential to know the difference between a service and a service application.
In PowerShell, a service
refers to a background process or system component that runs on a local/remote computer. Services are responsible for performing various tasks, such as running a web server, performing backups, or monitoring system performance.
These are managed using the Get-Service
, Start-Service
, Stop-Service
, and Restart-Service
cmdlets, which are part of the built-in PowerShell framework. We can use these cmdlets to retrieve a list of all the services on a local/remote computer, start
or stop
a service, or restart
a service.
A service application
is a specific type of service used in a SharePoint farm. These applications provide different functions and features to SharePoint, such as search, managed metadata, or user profiles. These are managed using the SharePoint PowerShell cmdlets, which are part of the SharePoint Management Shell.
We can use the Get-SPServiceApplication
cmdlet to retrieve a list of all the service applications in a SharePoint farm (we’ll be learning it in the following section), and the Start-SPServiceApplication
and Stop-SPServiceApplication
cmdlets to start or stop a service application.
So, a service
is a general term that refers to a background process or system component that runs on a computer. In contrast, a service application
is a specific type of service used in a SharePoint farm to provide various functions and features to SharePoint.
Using Get-SPServiceApplication
Cmdlet
To create and run service applications in PowerShell:
- Use the
Add-PSSnapin
cmdlet to add SharePoint PowerShell snap-in. - Use
Get-SPServiceApplication
to retrieve all service applications in the current farm. - Use
Where-Object
to filter the results received from Step 2 only to have service applications containingDisplayName
asManaged Metadata Services
. - Use the
if-else
block to check if the service application is not equal tonull
and show a message based on the condition.
NOTE: We must have SharePoint on our machine to use the following scripts.
1 2 3 4 5 6 7 8 9 10 11 |
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue $DisplayName="Managed Metadata Services" $ServiceApp = Get-SPServiceApplication | Where-Object { $_.DisplayName -eq $DisplayName} if($ServiceApp -ne $null){ $ServiceApp.Status }else{ Write-Host "Service application either not found or not created!" } |
The script above checked the status of the Managed Metadata Services
service application in SharePoint. First, we used Add-PSSnapin
to add the SharePoint PowerShell snap-in, which allowed us to use SharePoint cmdlets in the current PowerShell session.
Next, we set a variable called $DisplayName
to the string "Managed Metadata Services"
. It then used the Get-SPServiceApplication
cmdlet to get all service applications in the current farm and filtered the results using the Where-Object
cmdlet to only include service applications whose DisplayName
property matches the value of the $DisplayName
variable. This filtered list of service applications is stored in the $ServiceApp
variable.
The script then checks if the $ServiceApp
variable is not null
, which indicates that we found at least one service application. If $ServiceApp
is not null
, the script outputs the Status
property of the service application using the $ServiceApp.Status
expression.
If $ServiceApp
is null
, the script outputs a message indicating that the service application was either not found or not created.
If you are concerned about the current status of a service application, then the code fence below would be your solution.
1 2 3 4 |
$TypeName="Managed Metadata Service" $SeviceApp = Get-SPServiceApplication | Where-Object {$_.TypeName -eq $TypeName} |
We can use the $ServiceApp
variable to perform various operations on the service application, such as getting its properties, starting or stopping it, or deleting it. The following example demonstrates how you could use the $ServiceApp
variable to start the service application.
1 2 3 4 5 |
$TypeName="Managed Metadata Service" $ServiceApp = Get-SPServiceApplication | Where-Object {$_.TypeName -eq $TypeName} $ServiceApp | Start-SPServiceInstance |
That’s all about how to check if Service is running in PowerShell.