Last week I was looking at using PowerShell for creating and starting a Windows Service.

Usually I have a batch program for installing and uninstalling my services. As you can see, my Batch script is very basic, but works:

image

This type of script has worked for me for years but I’ve always wanted to learn PowerShell and figured this is a good time to do so and why not migrate to using PowerShell for installing my Windows Services.

After reading through the PowerShell documentation, Core Cmdlets and playing around with the PowerShell ISE tool (part of PowerShell 3), I found I was still lacking the knowledge to do this properly and since I was also learning PowerShell, I decided to see if anyone else had troubles with this or had insight and behold they did!

One of the best articles I came across nailed it perfectly with decent information on interacting with Windows Services from PowerShell and then a detailed walkthrough and example on exactly how to do it.

Referenced article: Create a Windows Service using PowerShell

Thanks, you saved me a lot of time.

Here is what I created for PowerShell scripts

After creating the Install.ps1 and Uninstall.ps1 files, to install your service, just right click on  Install.ps1 file and click on Run with PowerShell:

image

This will check if your service is already installed and if so, stop and uninstall your service. Then it will proceed with installing and started your service.

You can then confirm your service is installed and running by running the following Cmdlet as shown below:

Get-Service –Name “YourServiceName”

image

And voila, you can see that your service is installed and running. Pretty simple.

I will say that the scripts are far more complex than their original batch scripts, but as you will find, you have much more control and there is a lot you can do in PowerShell. It more than makes up for the extra effort.

Give it a try if you haven’t already started to use PowerShell. You may find you like it and that it can help in ways you never thought possible.

Below are each of the scripts.

Enjoy!

 

Install.ps1

$serviceName = "WorkManagerDemoService"
$serviceDisplayName = "WorkManager Demo Service"
$serviceDescription = "WorkManager Demo Service"
$serviceExecutable = "WindowsService1.exe"

# verify if the service already exists, and if yes remove it first
if (Get-Service $serviceName -ErrorAction SilentlyContinue)
{
    "service already installed, stopping…"
    # using WMI to remove Windows service because PowerShell does not have CmdLet for this
    $serviceToRemove = Get-WmiObject -Class Win32_Service -Filter "name=’$serviceName’"
    $serviceToRemove | Stop-Service
    $serviceToRemove.delete()
    "service removed"
}
else
{
    # just do nothing
    "service does not exist"
}

"installing service…"

# detect current execution directory
$directoryPath = Split-Path $MyInvocation.MyCommand.Path

# creating credentials which can be used to run my windows service
#$secpasswd = ConvertTo-SecureString "MyPa$$word" -AsPlainText -Force
#$mycreds = New-Object System.Management.Automation.PSCredential (".\MyUserName", $secpasswd)
# OR
#$myCredentials = Get-Credential

$binaryPath = $directoryPath + "\" + $serviceExecutable

# creating widnows service using all provided parameters, -displayName $serviceName, -credential $myCredentials
New-Service -name $serviceName -displayName $serviceDisplayName -binaryPathName $binaryPath -startupType Automatic -Description $serviceDescription

Start-Service -Name $serviceName

Get-Service $serviceName

"installation completed"

Pause

 

Uninstall.ps1

$serviceName = "WorkManagerDemoService"

# verify if the service already exists, and if yes remove it
if (Get-Service $serviceName -ErrorAction SilentlyContinue)
{
    "service already installed, stopping…"
    # using WMI to remove Windows service because PowerShell does not have CmdLet for this
    $serviceToRemove = Get-WmiObject -Class Win32_Service -Filter "name=’$serviceName’"
    $serviceToRemove | Stop-Service
    $serviceToRemove.delete()
    "service removed"
}
else
{
    # just do nothing
    "service does not exist"
}

Pause

Posted by Callon Campbell [MVP]

2 Comments

  1. Hey there! Great tutorial it worked very well for me and I was able to use it with some slight modifications. For your else loop, couldn’t you just throw in the logic you have for installing the service? This way the service can be installed the first time you run it and removed the second time (and so on) by invoking the first or second condition of the if-else? Thanks again!

    Reply

  2. Arun Jayaraj June 6, 2018 at 01:48

    Is there a way to install a new windows service with gMSA account?

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s