Stop Windows service command line
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 Show
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
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 in Windows Command LineTo List, all the Services in your Windows PC or Server, Perform the Following Steps
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 PowerShellIn 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
To List Only The Running ServicesGet-Service | Where-Object {$_.Status -eq "Running" } To List only the Stopped ServicesGet-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.
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 WindowsTo 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
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 FiltersSometimes, 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 LineWe 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
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 LineNow 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 LineWe 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
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
Follow us on Facebook or Twitter
Signup for Exclusive "Subscriber-only" Content
|