Window PowerShell provides a number of helpful CmdLets for managing windows services, such as
New-Service
Get-Service
Restart-Service
Resume-Service
Set-Service
Start-Service
Stop-Service
Suspend-Service
And create a new Windows Service using PowerShell “New-Service” CmdLet is very easy. The parameter description of CmdLet can be easily found on the MSDN website, so I will not provide it there. I will just provide syntax and an example of how it was used in my project.
The syntax for creating new windows service using PowerShell is the following
New-Service [-Name] <String> [-BinaryPathName] <String> [-Credential <PSCredential> ] [-DependsOn <String[]> ] [-Description <String> ] [-DisplayName <String> ] [-StartupType <ServiceStartMode> ] [ <CommonParameters>]
You can always type the command in your PowerShell window, but if you need to create the same service more than one time it’s better to create a script and save it somewhere, for example as part of the project you want to install.
Below is the example of the script I used in my recent project to create windows service using PowerShell
$serviceName = "MyService"
if (Get-Service $serviceName -ErrorAction SilentlyContinue)
{
$serviceToRemove = Get-WmiObject -Class Win32_Service -Filter "name='$serviceName'"
$serviceToRemove.delete()
"service removed"
}
else
{
"service does not exists"
}
"installing service"
$secpasswd = ConvertTo-SecureString "MyPassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential (".\MYUser", $secpasswd)
$binaryPath = "c:\servicebinaries\MyService.exe"
New-Service -name $serviceName -binaryPathName $binaryPath -displayName $serviceName -startupType Automatic -credential $mycreds
"installation completed"
I will explain the code in details:
If you already have the service installed, you probably want it to be uninstalled first (in fact you can just override your binaries and use the same service, but I prefer to delete/create service). So in line #3, I verified if the service already exists and if it exists I get the WMI object Win32_service and remove the service. Unfortunately, PowerShell does not have CmdLet to removing the service, so you have to remove the service using WMI
Line #11: If no service with the provided name exists just do nothing.
If your service does not require to be run under some service account you do not need the following code
$secpasswd = ConvertTo-SecureString "MyPassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential (".\MYUser", $secpasswd)
but in my case service should be running under a service account. In the code above I created the PSCredential object with username and password provided as plain text. But if for some reason your organization does not allow it, you can use the following code to ask for credentials from the user who will execute the script
$mycredentials = Get-Credential
in that case, you will get a window asking you for username and password
And finally, at line #19 you create a new Windows Service using PowerShell CmdLet New-Service providing all parameters you need
PowerShell script to create a new Windows Service can be downloaded from GitHub
More code samples at https://github.com/yaplex/CodeSamples
Alex Shapovalov is an independent contractor and founder of Yaplex – a Canadian custom software development company. Alex is a seasoned full-stack Software Developer who provides software development services in Canada and the USA.