How would I go about making a PowerShell Script into a Windows Service using the New-Service command?

  powershell, windows, windows-services

So I want to make a new windows service using a Powershell script that I wrote, that when run executes the powershell script. Here’s the command I want to run in order to make the service.

New-Service -Name "TestService" -StartupType Manual -BinaryPathName '"c:pathtopowershellpowershell.exe -File C:ScriptsPowerShellScript.ps1"'

When I run this command the service gets successfully created, but when I try this command:

Start-Service -Name "TestService"

I get an error stating Service cannot be started due to the following error:

Start-Service : Service 'TestService (TestService)' cannot be started due to the following error: Cannot start service TestService on
computer '.'.
At line:1 char:1
+ Start-Service -Name TestService
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand

Any thoughts?

Source: Windows Questions

LEAVE A COMMENT