Running windows service from command line

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.

How To Run Windows Update From Command Line

In Windows 10, Windows Update can be found in Settings –> Update & Security –> Windows Update. Normally Windows Update keeps on running in the background and will install new updates automatically.

You can also run Windows Update manually to check for new updates at any time. Here are three ways to check for new Windows updates.

Check for Windows updates using Windows Settings

To check for new updates and configure Windows Update settings, follow the steps below:

  1. Open Windows Settings (Windows key + i).
  2. Choose Update & Security.
  3. Choose Windows Update from the left hand menu.
  4. Press the Check for updates button in the right hand pane.

Check for updates

Run Windows Update from PowerShell (Command-line)

There is a PowerShell module for Windows Update. You can install and run the module to check for new updates. There are three steps for running Windows Update through PowerShell. Run the following commands step by step:

  1. Install-Module PSWindowsUpdate Install Module PSWindowsUpdate

This will install the Windows Update module in PowerShell.
Get-WindowsUpdate Get WindowsUpdate

This command will check for updates.
Install-WindowsUpdate Install WindowsUpdate

Читайте также:  Восстановление системного реестра windows

This command will install the available updates (which were listed in step 2)

Run Windows Update from Command Prompt (Command-line)

Windows Update can also be run through legacy CMD. The only limitation running Windows Update through command prompt is that it won’t show any progress. Only results are shown. Let’s see how to run it:

  • Go to Run –> cmd
  • Run the following command to check for new updates:
    wuauclt /detectnow
  • Run the following command to install new updates
    wuauclt /updatenow

Since the command prompt does not show any progress, a better approach would be to check and install updates at the same time. Here’s the command for this:

wuauclt /detectnow /updatenow

The above mentioned command will work in all versions of Windows including Windows 7 and Windows Server 2008 R2. But if you are using Windows 10 or Windows Server 2016, you can use UsoClient command which has more options than wuauclt. You can run UsoClient with the following switches:

StartScan – Start checking for updates

StartDownload – Start downloading updates

StartInstall – Start installing downloaded updates

RestartDevice – Restart Windows after updates are installed

ScanInstallWait – Check for updates, download available updates and install them

Hopefully this will be useful in situations where you want to automate certain Windows functions. What other purposes do you want to use command line options for running Windows Update?

Continue Reading:

Usman Khurshid is a seasoned IT Pro with over 15 years of experience in the IT industry. He has experience in everything from IT support, helpdesk, sysadmin, network admin, and cloud computing. He is also certified in Microsoft Technologies (MCTS and MCSA) and also Cisco Certified Professional in Routing and Switching. Reach him at Twitter @usmank11

14 thoughts on “ How To Run Windows Update From Command Line ”

Thank you Usman! Wow Microsoft has a new cli USOClient.exe awesome!

Thank you. Refurbished machine – needed to update in order to move from other machine to “new to me” machine after just getting ti to cooperate with the network. I have never run across a machine that would not update before. The cmd options did not work, but the PS option was exactly what I needed. Added to my bag of tricks. Thank you so much!!

usoclient.exe /StartScan does not update the shown ‘Last checked date’ shown in the GUI on a Windows Server 2016 or 2019.

PS C:\> Start-Service wuauserv -Verbose
PS C:\> $updateSession = new-object -com “Microsoft.Update.Session”;$updates=$updateSession.CreateupdateSearcher().Search($criteria).Updates
PS C:\> (New-Object -ComObject Microsoft.Update.AutoUpdate).DetectNow()

This is work on a Windows 10 – the Last checked time is updated in the GUI
BUT it’s not workting om Windows 2016 or 2019

Any know of a way to make this work on a Server 2016 ??

I don’t get any of these from article to work. Not even: Start-Service wuauserv -Verbose

PS C:\WINDOWS\system32> Start-Service wuauserv -Verbose
VERBOSE: Performing the operation “Start-Service” on target “wuauserv (wuauserv)”.
Start-Service : Failed to start service ‘wuauserv (wuauserv)’.
At line:1 char:1
+ Start-Service wuauserv -Verbose
+

+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
ServiceCommandException
+ FullyQualifiedErrorId : StartServiceFailed,Microsoft.PowerShell.Commands.StartServiceCommand

Please run PowerShell with administrative privileges.

usoclient StartScan does NOT make any changes to the Last checked date shown in the GUI
Neither does the Powershell command Get-WindowsUpdate

Читайте также:  Как отключить безопасную загрузку windows 10 asus

In previous Windows I had to updating the Last checked value in registry after performing a scan using the API (as they dont). But MS has moved the location to an unknown place i 2016 and 2019.

Anyone who knows how to update the “Last checked date” from CMD or API .

Function Force-WSUSCheckin($Computer)
<
Invoke-Command -computername $Computer -scriptblock < Start-Service wuauserv -Verbose >
# Have to use psexec with the -s parameter as otherwise we receive an “Access denied” message loading the comobject
$Cmd = ‘$updateSession = new-object -com “Microsoft.Update.Session”;$updates=$updateSession.CreateupdateSearcher().Search($criteria).Updates’
& c:\bin\psexec.exe -s \\$Computer powershell.exe -command $Cmd
Write-host “Waiting 10 seconds for SyncUpdates webservice to complete to add to the wuauserv queue so that it can be reported on”
Start-sleep -seconds 10
Invoke-Command -computername $Computer -scriptblock
<
# Now that the system is told it CAN report in, run every permutation of commands to actually trigger the report in operation
wuauclt /detectnow
(New-Object -ComObject Microsoft.Update.AutoUpdate).DetectNow()
wuauclt /reportnow
c:\windows\system32\UsoClient.exe startscan
>
>

Hi Glenn, thank you for such a detailed explanation.

Get-WindowsUpdate: The ‘Get-WindowsUpdate’ command was found in the module ‘PSWindowsUpdate’, but the module could
not be loaded. For more information, run ‘Import-Module PSWindowsUpdate’.
At line:1 char:1
+ Get-WindowsUpdate
+

+ CategoryInfo : ObjectNotFound: (Get-WindowsUpdate:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule

To remedy that, please try the following:
1. Run Set-ExecutionPolicy -ExecutionPolicy Unrestricted
2. Run Import-Module PSWindowsUpdate
3. Try running Get-WindowsUpdate again.
This worked for me.

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.

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.

Читайте также:  Nvidia geforce для windows 10 lenovo

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.

Оцените статью