Installing Windows Services Created with PowerShell
- Written by Alexander Riedel
- Last Updated: 09 September 2019
- Created: 05 March 2018
- Hits: 2022
PrimalScript includes a template for Windows PowerShell which makes writing Windows Services much easier. In this article, we will show you how to use MSI Builder to create an installer for your Windows Services.
We created the services packager engine to allow for self-install / uninstall of a service, but that is not always the best option. For example, if your service requires other files, you will most likely want to create an installer. It is quite easy to create an installer using the MSI builder, which is part of the PrimalScript and PowerShell Studio deployment package.
The MSI Builder had no special accommodation for Windows Services—until now. Since a service needs to be registered and started on install rather than just copied as a file, the MSI file needs to contain some extra information. Likewise, when uninstalling, the service needs to be stopped and unregistered before the file can be removed.
But fear not, there is not much for you to do. When you create the installer, simply select Windows Service as the ‘Product Type’, and you are almost done:
Almost—because there is one important detail that needs your attention. Since MSI does not know which file in your installer is a service and which is not, you need to make sure the actual service executable is the first file in the ‘Files’ list:
In future releases, we will add some more options for your service installers, but for this scenario, you simply need to select Windows Service as the ‘Product Type’ and choose the service executable as the first file. The installer will install your files, register the service, and start it. On uninstall it will stop the service and unregister it before it removes any files.
Note: Uninstalls can take longer for services since Windows Installer will wait for the service to stop. Depending on how you programmed the service, that can mean a 20 to 30-second delay.
Learning from a working example is always a little easier, so we updated last years SolitaireKiller service to use the new service template and create an installer for it. Download the code and working installer here.