Using powershell to create a new service

One of our clients has an application which always needs to run in the background, in the past the client resolved this by running the application after logon. Of course this meant after we perform maintenance the application would no longer work unless we’d log the user on. Autologon is not advised due to leaving a user logged in at the console.

Powershell has a neat function that can create a service from any executable, of course this does not mean you should try every application, but applications that require no input at all can be installed quite easily.

To create a new service you can use the following code, while running PowerShell as an administrator.

New-Service -BinaryPathName <EXECUTABLEPATH> -Name <NAMEOFTHESERVICE> -Credential <CREDENTIALSUSED> -DisplayName <DISPLAYNAME> -StartupType Automatic 

Of course this means that if we want to create a local system service of C:\Application.exe we can do the following:

$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "NT AUTHORITY\LOCAL SYSTEM"
New-Service -BinaryPathName C:\Application.exe -Name Application-AS-a-Service -Credential $credentials -DisplayName "Application as a service" -StartupType Automatic 

Tada! You’ve now installed the service. Remember that if the service needs access to a network share or other resources to assign the correct user to the service.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.