- Start or stop Windows service from command line (CMD)
- Create Windows service from executable
- 10 Answers 10
- How to test whether a service is running from the command line
- 14 Answers 14
- How do you run a command as an administrator from the Windows command line?
- 8 Answers 8
- Caveat: Enable the admin account
- How do I ‘run as’ ‘Network Service’?
- 5 Answers 5
Start or stop Windows service from command line (CMD)
We normally use Services.msc to start or stop or disable or enable any service. We can do the same from windows command line also using net and sc utilities. Below are commands for controlling the operation of a service.
Command to stop a service:
To start a service:
You need to have administrator privileges to run net start/stop commands. If you are just a normal user on the computer, you would get an error like below.
To disable a service:
To enable a service:
To make a service start automatically with system boot:
Note: Space is mandatory after ‘=’ in the above sc commands.
This SC command works on a Windows 7 machine and also on the down-level editions of Windows i.e Windows XP/2003 and Windows Vista. Again, if you do not have administrator previliges you would get the below error.
Note that the service name is not the display name of a service. Each service is given a unique identification name which can be used with net or sc commands. For example, Remote procedure call (RPC) is the display name of the service. But the service name we need to use in the above commands is RpcSs.
So to start Remote procedure call service the command is:
These service names are listed below for each service. The first column shows the display name of a service and the second column shows the service name that should be used in net start or net stop or sc config commands.
Create Windows service from executable
Is there any quick way to, given an executable file, create a Windows service that, when started, launches it?
10 Answers 10
To create a Windows Service from an executable, you can use sc.exe :
You must have quotation marks around the actual exe path, and a space after the binPath= .
Note that it will not work for just any executable: the executable must be a Windows Service (i.e. implement ServiceMain). When registering a non-service executable as a service, you’ll get the following error upon trying to start the service:
Error 1053: The service did not respond to the start or control request in a timely fashion.
There are tools that can create a Windows Service from arbitrary, non-service executables, see the other answers for examples of such tools.
Use NSSM( the non-Sucking Service Manager ) to run a .BAT or any .EXE file as a service.
- Step 1: Download NSSM
- Step 2: Install your sevice with nssm.exe install [serviceName]
- Step 3: This will open a GUI which you will use to locate your executable
Extending (Kevin Tong) answer.
Step 1: Download & Unzip nssm-2.24.zip
Step 2: From command line type:
C:\> nssm.exe install [servicename]
it will open GUI as below (the example is UT2003 server), then simply browse it to: yourapplication.exe
Many existing answers include human intervention at install time. This can be an error-prone process. If you have many executables wanted to be installed as services, the last thing you want to do is to do them manually at install time.
Towards the above described scenario, I created serman, a command line tool to install an executable as a service. All you need to write (and only write once) is a simple service configuration file along with your executable. Run
will install the service. stdout and stderr are all logged. For more info, take a look at the project website.
A working configuration file is very simple, as demonstrated below. But it also has many useful features such as and
these extras prove useful.. need to be executed as an administrator
If your service name has any spaces, enclose in «quotes».
I’ve tested a good product for that: AlwaysUp. Not free but they have a 30 days trial period so you can give it a try.
Same as Sergii Pozharov’s answer, but with a PowerShell cmdlet:
See New-Service for more customization.
This will only work for executables that already implement the Windows Services API.
You can check out my small free utility for service create\edit\delete operations. Here is create example:
Go to Service -> Modify -> Create
Executable file (google drive): [Download]
Probably all your answers are better, but — just to be complete on the choice of options — I wanted to remind about old, similar method used for years:
SrvAny (installed by InstSrv)
I created the cross-platform Service Manager software a few years back so that I could start PHP and other scripting languages as system services on Windows, Mac, and Linux OSes:
Service Manager is a set of precompiled binaries that install and manage a system service on the target OS using nearly identical command-line options (source code also available). Each platform does have subtle differences but the core features are mostly normalized.
If the child process dies, Service Manager automatically restarts it.
Processes that are started with Service Manager should periodically watch for two notification files to handle restart and reload requests but they don’t necessarily have to do that. Service Manager will force restart the child process if it doesn’t respond in a timely fashion to controlled restart/reload requests.
How to test whether a service is running from the command line
I would like to be able to query whether or not a service is running from a windows batch file. I know I can use:
but, this dumps out some text. What I really want is for it to set the errorlevel environment variable so that I can take action on that.
Do you know a simple way I can do this?
UPDATE
Thanks for the answers so far. I’m worried the solutions that parse the text may not work on non English operating systems. Does anybody know a way around this, or am I going to have to bite the bullet and write a console program to get this right.
14 Answers 14
Let’s go back to the old school of batch programing on windows
This will work everywhere.
if you don’t mind to combine the net command with grep you can use the following script.
You could use wmic with the /locale option
Thinking a little bit outside the box here I’m going to propose that powershell may be an answer on up-to-date XP/2003 machines and certainly on Vista/2008 and newer (instead of .bat/.cmd). Anyone who has some Perl in their background should feel at-home pretty quickly.
Another way, if you have significant investment in batch is to run the PS script as a one-liner, returning an exit code.
How do you run a command as an administrator from the Windows command line?
I have a small script that performs the build and install process on Windows for a Bazaar repository I’m managing. I’m trying to run the script with elevated, administrative privileges from within the Windows shell (cmd.exe)—just as if I’d right-clicked it and chosen Run as Administrator, but without using any method that requires use of the graphical interface.
8 Answers 8
A batch/WSH hybrid is able to call ShellExecute to display the UAC elevation dialog.
Press the start button. In the search box type «cmd», then press Ctrl + Shift + Enter
All you have to do is use the runas command to run your program as Administrator (with a caveat).
In my case, this was
Note that you must use Quotation marks, else the runas command will gobble up the switch option to cmd.
Also note that the administrative shell (cmd.exe) starts up in the C:\Windows\System32 folder. This isn’t what I wanted, but it was easy enough to pass in the current path to my installer, and to reference it using an absolute path.
Caveat: Enable the admin account
Using runas this way requires the administrative account to be enabled, which is not the default on Windows 7 or Vista. However, here is a great tutorial on how to enable it, in three different ways:
I myself enabled it by opening Administrative Tools, Local Security Policy, then navigating to Local Policies\Security Options and changing the value of the Accounts: Administrative Account Status policy to Enabled, which is none of the three ways shown in the link.
How do I ‘run as’ ‘Network Service’?
I am trying to run a process as another account. I have the command:
but then this asks for the password. However there is no password set for the network service.
Is what I am trying to do possible?
5 Answers 5
Use PsExec.exe from SysInternals, running from an elevated command prompt.
e.g. this will open a new command prompt running as NETWORK SERVICE:
this will run it as LOCAL SYSTEM:
You can verify these by running whoami from the cmd prompt.
In Task Scheduler, create a task to run the application under the NETWORK SERVICE user. You can then run the task from the command line using
Where taskname is the name of your task.
You can only impersonate as service account from a Windows service typically, like this post mentions:
The trick is to run your code as Local System and from there you can impersonate the service accounts by using the appropriate username with no password. One way to run your code as the Local System account is to create a command line shell by using the technique shown below (taken from this orginal post), and execute your assembly from there. Calling System.Diagnostics.Debugger.Break() in your code allows you to debug.
To create a command-line shell that runs under the local system account, open a new command line window and enter:
A new command window should have opened up. In that window run your application.exe — you’ll see that you’re now running as the built-in System user account. After you’ve finished testing, you can delete the test service you created by entering:
If you try to do that in your own user context, then such attempts should fail.