• Home /
  • Blog /
  • Create a windows service using PowerShell
Powershell Create New Service

Create a windows service using PowerShell

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 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 at 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 service, so you have to remove 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 reasons your organization does not allow it, you can use the following code to ask 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

powershell cmdlet to provide credentials

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

Alex Shapovalov is a founder of Yaplex - an IT consulting and software development company, located in Mississauga, Ontario. Alex provides IT consulting and .NET software development services in Canada and the USA.

Yaplex - Freelance developer - white logo on black background

Yaplex ® is a custom software development company located in Mississauga, Ontario. Yaplex was founded in 2013 and provides website development, mobile development, and custom .NET software development services.

Contact Information

Mississauga, ON, Canada
Phone: +1 647 328 3809
Email: hello@yaplex.com