Running PowerShell Script (*.PS1) as a Windows Service

Any PowerShell script could be remodeled to a actual Windows service that runs within the background and begins routinely throughout your server boot. You can create a Windows service utilizing srvany.exe or instsrv.exe instruments (from Windows Server Resource 2003 Kit) which let you run the powershell.exe course of with a parameter that incorporates the trail to your PS1 script file.

The fundamental drawback of making a service utilizing this technique is that srvany.exe doesn’t management a PowerShell script execution state, and if the app crashes (hangs up), the service doesn’t see it and goes on working. To create a Windows service from a file that incorporates a PowerShell script, on this article we’ll use the NSSM (Non-Sucking Service Manager) toolkit, which doesn’t display the above talked about disadvantages.

You can obtain and set up NSSM manually or utilizing Chocolatey. Firstly, set up Choco itself:

Set-ExecutionPolicy Bypass -Scope Process -Force; `
iex ((New-Object System.Net.WebClient).DownloadString(' up.ps1'))

Then set up the NSSM package deal:

choco set up nssm

In this instance, we’ll monitor the adjustments in a particular Active Directory group in actual time and inform a safety administrator utilizing a and e-mail (the script is given in ) .

So, now we have a PowerShell code that must be saved as a PS1 file. Let’s add an infinite loop that performs a verify each minute:


Of course, to implement such a situation you may within the Task Scheduler. But if you need to reply to adjustments in actual time, the separate service technique is healthier.

You can create a service from a PowerShell script utilizing NSSM immediately from PowerShell:

$NSSMPath = (Get-Command "C:psnssmwin64nssm.exe").Source
$NewServiceName = “CheckADGroup”
$PoShPath= (Get-Command powershell).Source
$PoShScriptPath = “C:psCheckADGroupcheckad.ps1”
$args = '-ExecutionPolicy Bypass -NoProfile -File ""' -f $PoShScriptPath
& $NSSMPath set up $NewServiceName $PoShPath $args
& $NSSMPath standing $NewServiceName

Start your new service:

Start-Service $NewServiceName

Check the service standing in PowerShell:

Get-Service $NewServiceName

running powershell script as a windows service

So you may have created and began your new Windows service. Make certain that it has appeared within the providers administration console (providers.msc).

CheckADGroup has appeared, it’s configured to start out routinely and is at present working. As you may see, your PowerShell script is working contained in the nssm.exe course of.

windows service from a powershell script using nnm

Please word that the service is working beneath the System account. If you employ different modules in your PowerShell scripts (in my case, from Active Directory for Windows PowerShell is used to get the checklist of members within the area safety group), this account should have entry to the PS module information and AD connection permissions (in my case). You may begin this service beneath one other area account (or a account) and if they don’t have native admin rights.

In order the service can present notifications in a consumer session allow the Allow service to work together with desktop possibility on the Log on tab.

To make it work in Windows 10 and Windows Server 2012 R2/2016, change the DWORD NoInteractiveServices parameter worth within the registry key HKEY_LOCAL_MACHINESystemCurrentControlSetManagementWindows to zero and run the Interactive Services Detection Service:

Start-Service -Name ui0detect

However, Interactive Services Detection Service has been fully faraway from Windows 10 construct 1803, and also you gained’t be capable of swap to Session zero. So you gained’t see the notification home windows displayed beneath System account.

You can change the service description utilizing this command:

& $NSSMPath set $NewServiceName description “Monitoring of AD group adjustments”

To take away the service you may have created, use the sc delete command or:

nssm take away CheckADGroup

nssm remove powershell service

Check Also

How to Use Native SSH Client in Windows 10?

The built-in SSH shopper appeared in Windows 10 and Windows Server 2019. Ssh.exe can be …

Leave a Reply

Your email address will not be published. Required fields are marked *