Windows Service management through the command line is really a good approach when you want to manage plenty of services and perform day to day actions like stop, start and restart
I think you would agree, If I say GUI
is fun for entry-level but when it comes to performing the job smartly and creating automation for efficiency. Command-line
is your key
PowerShell
has a lot of commands to help us manage the windows server better and create automation and do the boring (or) repetitive tasks swiftly
In this article, we are going to see How to Manage Services from the Windows Command line using PowerShell. We are going to see various examples of How to List , Stop, Start, Restart a Single Service or multiple Services.
To Manage the Services in Windows, We have a pack of Powershell commands and each does a unique job in the Windows Service Management. It helps us perform our day to day needs like Stopping, Starting, Restarting, Listing, Searching, etc
In this article, we are going to see various Windows Powershell commands such as
- Get-Sevice
- Stop-Service
- Start-Service
- Where-Object
- Restart-Service
Not just this, There are few more and look at the index to know what this article is packaged with
I am thrilled and I hope you are too. Let's march on.
Index
- How to List the Services Windows Command Line
- How to List only Running or Stopped Services in PowerShell
- How to List a Service or Get Service by Name in Windows
- How to Search for the Service[s] by Status, DisplayName, Search String etc.
- How to Stop the Service[s] in Windows Command Line
- How to Start the Service[s] in Windows Command Line
- How to Restart the Service[s] in Windows Command Line
How to List the Services in Windows Command Line
To List, all the Services in your Windows PC or Server, Perform the Following Steps
- Open PowerShell Terminal or PowerShell ISE as Administrator
- Type
Get-Service
in the Terminal
You would be presented with all the available Services on the Windows Machine
The result would container three columns as shown below, Status, Name, and DisplayName
You can search or List a Single or Multiple Services based on any of these columns, which we will see in upcoming sections on this article.
PS C:\Users\sarav>
Get-Service
Status Name DisplayName ------ ---- ----------- Stopped AarSvc_ba23f Agent Activation Runtime_ba23f Stopped AJRouter AllJoyn Router Service Stopped ALG Application Layer Gateway Service Stopped AppIDSvc Application Identity Running Appinfo Application Information Stopped AppReadiness App Readiness Running AppXSvc AppX Deployment Service (AppXSVC) Running AudioEndpointBu... Windows Audio Endpoint Builder Running Audiosrv Windows Audio Stopped autotimesvc Cellular Time Stopped AxInstSV ActiveX Installer (AxInstSV) Stopped BcastDVRUserSer... GameDVR and Broadcast User Service_... Stopped BDESVC BitLocker Drive Encryption Service
How to List only Running or Stopped Services in PowerShell
In this section we are going to see how to list the windows services based on a Specific State they are in.
To List, Either only Running and Stopped Services, PowerShell Get-Service
Command can be used along with one more Filtering command named Where-Object
.
It acts like a grep
of Linux and it does the job so perfect and precise
So to List Running or Stopped Services in Windows Command line you should do the following
- Open PowerShell Terminal or PowerShell ISE as Administrator
- Use one of the following command based on your requirement
To List Only The Running Services
Get-Service | Where-Object {$_.Status -eq "Running" }
To List only the Stopped Services
Get-Service | Where-Object {$_.Status -eq "Stopped" }
In fact, You can Use any of the Following State Value
in place of Running
or Stopped
to get the Services in that State.
Value | Meaning |
---|---|
ContinuePending | The service has been paused and is about to continue. |
Paused | The service is paused. |
PausePending | The service is in the process of pausing. |
Running | The service is running. |
StartPending | The service is in the process of starting. |
Stopped | The service is not running. |
StopPending | The service is in the process of stopping. |
For example, If you would like to Get a Service which is in Paused
State then your command should be like this
Get-Service | Where-Object {$_.Status -eq "Paused" }
How to List a Service or Get Service by Name in Windows
To List or to Get a Service by Name you have to be aware of the Name of the Service or at least a part of the Service name as we can use *
wildcard to find the rest.
To List or to Get Service by Name do the following
- Open PowerShell Terminal or PowerShell ISE as Administrator
- Use the following
Get-Service
the command along with a-Name
(or)-DisplayName
parameter
To List a Service named Jenkins
I can use any of the following commands and Be informed that Service Name is Case Insensitive
Get-Service -Name jenkins (or) Get-Service -Name jenkins (or) Get-Service -DisplayName jenkins (or) Get-Service -Name JEnKins (or) Get-Service -DisplayName JEnKins (or) Get-Service -Name jen*s
How to Search for the Service[s] by More Filters
Sometimes, Our requirement would not be simpler as we think, It might get complicated when we get a requirement like
We might have to list (or) restart all the tomcat instances running on the server and exclude instance which contains a Specific String in its name
Let's Suppose, that we have a Windows Server with N number of Tomcat Services (instances) and they are named after their Environment name they belong to like dev, uat etc. like Dev_Tomcat1, Test_Tomcat2, Uat_Tomcat4 and so on.
Now to list only the DEV
and UAT
instances and not SIT
we would have to use some more filters other than just Name
or DisplayName
Here are some examples related to this type of scenario.
# All these examples made based on the presence of # Environment Names `SIT` `UAT` `Dev` in the Service Name of # Service Display Name # The Search is By Default CASE INSENSITIVE # Find Tomcat Instances belong to Test Environment Get-Service -DisplayName "*Tomcat*" -Include "*Test*" (or) Get-Service -DisplayName "*Tomcat*" -Include "*tEst*" ---- # Find All Tomcat Instances EXCEPT the ones belong TEST Environment Get-Service -DisplayName "*Tomcat*" -Exclude "*Test*" -- – # You can also add STATUS Filter into this command Get-Service -DisplayName "*Tomcat*" -Exclude "*Test*"|Where-Object {$_.Status -eq "Running"}
How to Stop the Service[s] in Windows Command Line
We have so far seen, how to list the services in windows machine (PC or Server) using the Powershell command line.
Now we are going to see, How to Stop the Service[s] in Windows PowerShell Command Line
Now let us Split this Part into two as follows
- How to Stop a Single Service by Name
- How to Stop One or More Services matching the Query (or) Search term
Despite you are stopping a Single Service or Multiple Services. You have to first list the Services with Get-Service
with necessary Filters like -Name
or Status
etc.
Once the result is presented, With the help of pipe
|
symbol you pass all the services to an another Command called Stop-Service
Stop-Service
command is responsible to stop the service (or) Services
Simply put, to Stop the Service or Services. You just need to list it first and make sure thats what you want to be stopped and then redirect it to Stop-Service
with the help of pipe
Here are some of Windows Stop Service Example commands
# Simply Stop the Service named Jenkins Get-Service -Name Jenkins|Stop-Service --- # Stop all Running Services Get-Service|Where-Object {$_.Status -eq "Running"}|Stop-Service --- # List and Stop All Running *Tomcat* Services Get-Service -DisplayName "*Tomcat*"|Where-Object {$_.Status -eq "Running"}|Stop-Service --- # List and Stop All Running Tomcat Services, # Only Production, No DEV, UAT, SIT ( We Presume Display Name Contains the Environment Name) Get-Service -DisplayName "*Tomcat*" -Exclude "*DEV*" "*SIT*" "*UAT*"|Where-Object {$_.Status -eq "Running"}|Stop-Service
How to Start the Service[s] in Windows Command Line
Now we are going to see, How to Start the Service[s] in Windows PowerShell Command Line
Despite you are Starting a Single Service or Multiple Services. You have to first list the Services with Get-Service
with necessary Filters like -Name
or Status
etc.
Once the result is presented, With the help of pipe
|
symbol you pass all the services to another Command called Start-Service
Here are some of Windows Start Service from Command Line examples
# Simply Stop the Service named Jenkins Get-Servicec -Name Jenkins|
Start-Service
--- # Stop all Running Services Get-Service|Where-Object {$_.Status -eq "Running"}|
Start-Service
--- # List and Stop All Running *Tomcat* Services Get-Service -DisplayName "*Tomcat*"|Where-Object {$_.Status -eq "Running"}|
Start-Service
--- # List and Stop All Running Tomcat Services, # Only Production, No DEV, UAT, SIT ( We Presume Display Name Contains the Environment Name) Get-Service -DisplayName "*Tomcat*" -Exclude "*DEV*" "*SIT*" "*UAT*"|Where-Object {$_.Status -eq "Running"}|
Start-Service
How to Restart the Service[s] in Windows Command Line
We have just learned how to Stop and Start the services, Now it is a time to learn How to Restart Service from Windows Command Line
To Restart windows Service Command Line do the following
- Open PowerShell Terminal or PowerShell ISE as Administrator
- Use the following
Get-Service
the command along with a-Name
(or)-DisplayName
parameter and List the Services you want to be restarted - In the same Command add a
pipe |
symbol at the suffix along with a commandRestart-Service
To Restart Windows Service from Command Line, First we need to list the services that we want to be restarted using Get-Service
we can customize and Search for the Services you want using Get-Service
parameters like Name
and DisplayName
, Status
etc
Once we have the list ready with Single or Multiple Services that we want to restart.
We can use another command, Given dedicatedly to restart services named Restart-Service
In most cases, we would like to have more control on the Restart process, in such cases, you can try to Stop
and Start
the services using Stop-Service
and Start-Service
commands rather directly using Restart-Service
Here are few examples of How to restart the Service in Windows Command Line
# Simply Stop the Service named Jenkins Get-Servicec -Name Jenkins|
Restart-Service
--- # Stop all Running Services Get-Service|Where-Object {$_.Status -eq "Running"}|
Restart-Service
--- # List and Stop All Running *Tomcat* Services Get-Service -DisplayName "*Tomcat*"|Where-Object {$_.Status -eq "Running"}|
Restart-Service
--- # List and Stop All Running Tomcat Services, # Only Production, No DEV, UAT, SIT ( We Presume Display Name Contains the Environment Name) Get-Service -DisplayName "*Tomcat*" -Exclude "*DEV*" "*SIT*" "*UAT*"|Where-Object {$_.Status -eq "Running"}|
Restart-Service
So This is how Windows PowerShell commands help us to manage the Windows services from Command line, We learned how to List, stop, start and restart windows services from command line
With this command line, We can stop, start, restart Multiple services at once in bulk that's what I like the most about it.
If you have any questions for me. Please feel free to comment
Rate this article [ratings] Share it with your friends if you find it worth
Cheers
Sarav AK
Follow me on Linkedin My Profile Follow DevopsJunction onFacebook orTwitter For more practical videos and tutorials. Subscribe to our channel
Signup for Exclusive "Subscriber-only" Content