Category filter

PowerShell scripts to manage services on Windows

Windows Services are like computer applications, but they operate without a user interface. From the moment the system boots up, these services start running in the background without any manual user interaction. Managing services involves a range of operations, including creating, starting, stopping, restarting, suspending, modifying service properties, and deleting services. Organizations can benefit from managing services on their Windows devices. For instance, stopping certain services in specific scenarios can boost system performance, reduce the load, and turn off unnecessary operations on the device. Additionally, IT administrators can create services to maintain Active Directory services for domain management and set DNS services to maintain IP addresses. Using Hexnode’s Execute Custom Script remote action, IT administrators can deploy the PowerShell scripts to manage services on a fleet of Windows devices remotely.

Disclaimer:


The sample scripts provided below are adapted from third-party open-source sites.

PowerShell script

Get the Services on the device

The above script can fetch the list of all the services present on the Windows device along with their current statuses.

Create new Service

You can use the following script to create a new Windows Service. IT administrators can create a service to support their organizational applications in the background. This service will start running the moment the system is powered up, regardless of whether a user logs in. This ensures that the applications receive support from the moment the system boots up.

To create a new service, deploy the above script by replacing ‘newservicename‘ with a suitable name and ‘C:\Path\To\Executable.exe‘ with a path to store the service file.

Start a Service

Upon creating a new service, it has to start to run on the device. A service can be started remotely by executing the following PowerShell script.

To start a service, execute the above script by replacing ‘newservicename‘ with the name of the service.

Stop a Service

Cutting down on unnecessary services can help reduce the load on the system. The following script can help stop running services on a Windows device.

To stop a service, execute the above script by replacing ‘newservicename‘ with the name of the service.

Restart a Service

In some cases, services may not function as expected and may produce improper results. In such cases, it is advisable to restart the service for better performance. The following script can be used to stop and restart a service on Windows devices.

Execute the above script to restart a service and replace ‘newservicename‘ with the name of the service.

Suspend a Service

When a service is stopped and needs to be started again after some time, it may consume a significant amount of system resources as it needs to be reinitialized from the start. A better alternative in such cases is to suspend or pause the service. Suspending a service preserves its state and cache data internally, so when it is resumed, it can pick up from where it left off, without requiring a full reinitialization.

The above script can be used to suspend or pause a service after replacing ‘newservicename’ with the name of the service.

Change the properties of a Service

Change startup type

The startup type determines how a service behaves at the time of boot-up on a Windows device. There are three options to choose from:

  • Automatic – Starts the service automatically as soon as the system boots up.
  • Manual – The service can be started manually by the user or by another service or application.
  • Disabled – Prevents the service from starting up until this setting is modified.

The above script can change the startup type of a service by replacing Type with either one of the three options which are “automatic”, “manual” and “disabled”. Also, the actual name of the service should replace newservicename before executing the script.

Change Service display name

A service display name is the name that shows on the Services Control Manager panel.

Using the above script, the display name of a service can be modified as required. Before executing the script make sure that the actual service name replaces newservicename and the display name is added in the place of newdisplayname.

Change Service description

A service description can be set to describe the functionality of the service, and it is displayed in the Services Control Manager panel when a service is selected.

Using the above script, the description of a service can be modified as required. Before executing the script make sure that the actual service name replaces newservicename and the description is added in the place of newdescription.

Remove a Service

A service can be deleted from the system to reduce the load on the system for better system optimization.

The above script removes or deletes a service. Replace “newservicename” with the actual service name before execution.

What happens on the device end?

After creating a service, it will be added to the list of services in the Services Control Manager panel. To access the Services Control Manager panel, you can either click on the Windows key and search for Services or press Windows key + R to open the Windows Run dialog box and type ‘services.msc’ and hit enter.

Once you open the Services Control Manager panel, you can find the newly created service (Mitsogo.Inc in this case) listed among the other services on the device.

Service Manager Control panel to refresh the changes made by the PowerShell scripts to manage services on Windows devices

You can observe all the changes made to the services, such as the startup type, display name, description, and status, in this control panel.

After making changes to a service, the Services Control Manager panel may need to be refreshed for the changes to take effect. To refresh the Services Control Manager panel, click on the Refresh symbol in the top left corner of the panel.

Notes:

  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository