5000+ Great Articles

What Do I Need to Create a Windows Service?

What Do I Need to Create a Windows Service?.

Sometimes you need an application or script to keep working regardless of whether you are logged into your computer or not. Maybe it’s a PowerShell script for monitoring ports, or a web server that hosts a page on your home network.

The point is, if you want a process, script or program to run while the computer is on, you need to create a Windows service.

What Do I Need to Create a Windows Service?

To create a Windows service in Windows 10, several prerequisites must be met:

What Is the Non-Sucking Service Manager?

Yes, one cannot give such a name without explanation. Of course, the name is unfortunate, but it is accurate. The No Suction Services Manager (NSSM) is perhaps the simplest way to create a Windows service that is both highly reliable and customizable. In addition, it is free and open source software (OSS).

NSSM can be used through the Windows command line or graphical user interface (GUI). This means that anyone can use it. NSSM can be used on any version of Windows starting from Windows 2000 inclusive. There are 32-bit and 64-bit versions. If you are using a 64-bit computer, please try this version first. If doesn’t work, go back to 32-bit.

You can download NSSM from the website, clone NSSM from Git, or install NSSM using Chocolatey. Chocolatey is a package manager for Windows. The installation methods depend on the route you choose. See NSSM Instructions. In our example, we download from the NSSM website and install it to C: WINDOWS system32.

– /

Create a Windows Service With NSSM

In this example, we will create a service from a PowerShell script to log the average CPU utilization.

  1. Copy and save this script as log-CPULoadPercentage.ps1 in a location that is unlikely to be accessible to anyone else. Try to create a C: / Scripts directory and save it there. Also, create a folder in your scripts called Logs. Note that the path to the script is C: /Scripts/log-CPULoadPercentage.ps1. You will need this later.

Note. All lines below followed by # are comments and do not affect the script.

CLS # Optional. I like to use this to clear the terminal when testing. # Make sure you have a Logs folder in the same directory as this script. # The log will keep records. Start-Transcript -Path “$ PSScriptRoot Logs log-CPULoadPercentage – $ (get-date -f yyyy-MM-dd) .txt” -Append # While the loop continues to run until manually stopped While ($ True) {#Creates a timestamp to see when the measurement was taken $ timeStamp = get-date -f yyyy-MM-h: mm: ss # Gets the average load percentage at this time, then waits 5 seconds to do it again … $ cpuLoadPercent = Get-CimInstance win32_processor | Measure-Object -Property LoadPercentage -Average | Select-Object Average; Start-Sleep -Seconds 5 # Isolates only the mean so that there is no strange line @ {Average = 13} $ cpuLoadPercent = $ cpuLoadPercent.Average # prints the results to the screen or, in this case, to the log Write-Host “$ timeStamp CPU Load Percentage $ cpuLoadPercent “} Stop-Transcript

  1. This can be done either from the Windows Command Prompt or PowerShell. Open it as administrator.
  2. Enter the nssm install logCPUAvg command and run it. The NSSM Service Installer window opens.

  1. Click the ellipsis button next to the Path: field and navigate to powershell.exe, which is usually located in the C: Windows System32 folder. Select powershell.exe. The “Path:” and “Launch directory:” fields will be filled in automatically.

  1. Enter the following in the Arguments: field: -ExecutionPolicy Bypass -NoProfile -File “C: PathToScript get-Script.ps1” where the last part is the path to your PowerShell. script and script name.

  1. Click the Details tab. Enter what you want the service to appear in the Windows Service Manager in the Display Name: field. Then enter what it does in the Description: field. Startup type: can be set to automatic, automatic (delayed start), manual or disabled. Automatic mode is suitable for this exercise.

  1. Select the Login tab. Select the radio button “This account:” and enter the account and password under which the service will run. You will need to select an account under which the service will run. Ideally, you should have a Windows account created just to run this service. This account’s permissions should only be limited to what the service needs to do. You can choose the Local System account, but this is not recommended for security reasons.

There are several other tabs that you can use to configure the service. The defaults on these tabs are sufficient for this exercise. Click the Install Service button.

  1. After installing the service, you will see that the “logCPUAvg” service has been successfully installed! window. Select OK to close it. This completes the installation.

  1. Open Windows Service Manager and verify that the service is present.

  1. Start the service to make sure it will work.

  1. To make sure this service is running, use Explorer to navigate to the location where you want to save the log and check if it exists.


Removing a Windows Service With NSSM

You may no longer need to monitor your CPU usage, so you want to get rid of this service. Fortunately, NSSM makes this easy.

  1. In Windows Service Manager, stop the service. Do this by selecting the CPU Average Log Service and then either clicking the square stop button in the toolbar or the Stop Service link on the left.

  1. Open a Windows Command Prompt or PowerShell as administrator.
  2. Enter the nssm remove logCPUAvg command and run the command.

  1. NSSM will ask you for confirmation. Select “Yes”.

  1. After removing the service, you will see that the “logCPUAvg” service has been removed successfully! The confirmation. Click OK and you’re done.

That’s all. It is recommended that you check the service manager to make sure the service is no longer there. If you still see the service, you may need to refresh your screen and it should be gone.

Services are a great way to run applications or scripts that need to run constantly, reboot if they crash, or require privileges other than the current user. If you don’t need your application or script to perform all of these tasks, consider using a scheduled task.

What Do I Need to Create a Windows Service?

Exit mobile version