How to Manage Windows Services via Command Line

Windows Services

I always look for efficient ways to manage system tasks without leaving the terminal, even when I am using Windows. On Windows, services run in the background to keep the system and apps working smoothly. Instead of opening the Services Manager, I prefer to manage the Windows Services via the command line. In this guide, we’ll explore these methods and see how you can manage Windows services right from the terminal.

1. Manage Windows Services with sc.exe

sc.exe is a built-in command-line tool for managing Windows services. It lets you configure, query, and control services directly from the terminal. With sc.exe, you get full control over Windows services without needing the graphical Services Manager.

Checking service status with sc

We can use the sc query serviceName command to check the status of a specific service. For example, we run the sc query MySQL80 command to retrieve details about the MySQL80 service, including its state:

check service status with sc

Currently, MySQL is not running on our machine.

Starting a service with sc

To start a specific service using sc.exe, we can use the sc start ServiceName command. For example, we run sc start MySQL80 to start the MySQL80 service. To verify if the service has started successfully, we can check its status using the sc query MySQL80 command:

Start Service Sc

Stopping a service with sc

You can stop a service to free up system resources. For example, the command sc stop MySQL80 stops MySQL, which can be verified using the sc query MySQL80 command:

Stop Service Sc

Creating a new service with sc

We can create a new service using the sc create command. This requires specifying the service name, executable path, and startup type. For example, to create a new service named “mte” that will start automatically at boot, type:

sc create mte binPath= "C:\Users\HP\Desktop\Examples\Service.exe" start= auto
Create Service Sc

Updating a service with sc

We can use the sc config command to configure an existing service. For example, to change the startup type to manual, run the command:

sc config serviceName start= demand
Change Service Sc

Deleting a service with sc

Once a service is no longer needed, we can remove it permanently from Windows using the command:

sc delete srviceName
Delete Service Sc

2. Manage Windows Services With Net Command

The net command in Windows enables us to manage services from the command line. It allows users to start, stop, pause, resume, and query services without using the graphical Services Manager. 

Starting and stopping service with net command

We can start or stop Windows services with the net start serviceName and net stop serviceName commands, respectively:

Start or stop service with net command

Pausing and resuming services with net command

Some Windows services support pausing and resuming instead of a complete stop. In that case, we can use the net pause ServiceName and net continue ServiceName commands, respectively:

Pause and resume service net command

Checking service status with net command

The net command itself does not provide a direct way to check the status of a specific service, but we can use it along with the findstr command to filter the results. For example, to check if the specified service is running, type:

net start | findstr "ServiceName"
Check service status with net command

If the specified service is running, the command returns its name; otherwise, there is no output.

Manage services remotely with net command

We can use the net command to manage services on remote computers by specifying the computer name. For example, the net start ServiceName /S RemotePC and net stop ServiceName /S RemotePC commands are used to start or stop services on a remote computer.

3. Manage Windows Services with PowerShell Cmdlets

PowerShell offers more advanced control over Windows services with built-in cmdlets like Get-Service, Start-Service, Stop-Service, and Restart-Service. These cmdlets allow users to check the status of a service, start or stop it, and even restart it when needed.

PowerShell cmdlets provide detailed output, including service status, display name, and dependent services. These cmdlets allow scripting and automation, making it useful for managing multiple services efficiently.

PowerShell’s flexibility and powerful features make it the preferred tool for administrators managing Windows services.

Getting service status with cmdlets

We can use the Get-Service -Name ServiceName cmdlet to get the details about the specified service. For example, the following command returns the status of the MySQL80 service, showing whether it is running or stopped:

Get-Service -Name MySQL80
Check Service Status Get Service

Querying services with PowerShell cmdlets

We can use the Get-Service command to query the services based on specific criteria. For example, we can retrieve all currently running services using the command:

Get-Service | Where-Object {$_.Status -eq 'Running'}
Query Specific Services

Starting and Stopping Services with PowerShell cmdlets

We can start or stop a specific service using the Start-Service and Stop-Service cmdlets respectively. For example, we use PowerShell commands to start, stop, and check the status of the MySQL80 service:

Start-Service -Name MySQL80 
Stop-Service -Name MySQL80
Get-Service -Name MySQL80
Start Stop Service Powershell

Changing service startup type with PowerShell cmdlets

PowerShell cmdlets enable us to update the service startup type. For example, we can run the following cmdlets to configure a service to start automatically with the system, require manual startup, or be disabled, respectively:

Set-Service -Name ServiceName -StartupType Automatic
Set-Service -Name ServiceName -StartupType Manual
Set-Service -Name ServiceName -StartupType Disabled

These cmdlets help administrators manage service behavior efficiently, which ensures that essential services start as needed while preventing unnecessary ones from running.

Managing remote services with cmdlets

PowerShell also allows managing services on remote computers by specifying the names of the remote computers. For example, the Get-Service -Name ServiceName -ComputerName RemotePC command retrieves the status of a specific service running on a remote computer named RemotePC, allowing administrators to monitor services remotely.

Similarly, the Restart-Service -Name ServiceName -ComputerName serviceName command attempts to restart the specified service on a remote computer. It ensures that the service restarts without requiring direct access to the machine. However, remote service management requires appropriate permissions and PowerShell remoting to be enabled.

4. Automate Service Management Tasks

PowerShell scripts allow automating service management, making it easier to monitor and control Windows services without manual intervention. We can create a script that continuously checks the status of a specific service and restarts it if it stops unexpectedly.

For example, the following script checks whether the MySQL80 service is running and restarts it if it is stopped:

$serviceName = "MySQL80"
$service = Get-Service -Name $serviceName
if ($service.Status -ne "Running") {
Restart-Service -Name $serviceName -Force
Write-Output "$serviceName was stopped and has been restarted."
} else {
Write-Output "$serviceName is already running."
}

By default, Windows restricts running scripts. To enable execution, first, execute the command:

Set-ExecutionPolicy RemoteSigned
Enable Script Execution

Now navigate to the script location and run the .\serviceScript.ps1 command to run the script:

Run Script

Managing Windows services from the command line gives you complete control without relying on the graphical Services Manager. Whether you use sc.exe, net, or PowerShell cmdlets, each method offers efficient ways to start, stop, and configure services.

PowerShell’s scripting capabilities make automation easy, allowing you to monitor and manage services seamlessly. By mastering these tools, you can troubleshoot issues, optimize system performance, and ensure critical services run smoothly.

For more ways to optimize your Windows system and troubleshoot common issues, check out these guides on fixing Windows problems and optimizing your PC for better performance.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Anees Asghar Avatar