Creating a Windows Service Using PowerShell

May 21, 2013

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

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Software Engineering

Web development

PowerShell, Programming and DevOps

Musings and mischief on PowerShell, Programming and DevOps.

toon vanhoutte

Blogging about connecting devices, systems and people! On-premises and in the cloud. Microsoft stack only.

Ken Cenerelli

My life in software development

STEFANROTH.NET

Things about cloud and management

Build HoloLens

All about HoloLens

DevDays®

For web designers and cloud developers

Build Azure

All about the Microsoft Cloud

vishal patel

Software Developer, Windows 8 and Windows Phone Enthusiast

SQL with Manoj

SQL Server (TSQL) Programming, DB concepts, Tips & Tricks with >400 articles... comments welcome!!!

dragablz.wordpress.com/

Dragable, tearable, dockable WPF TabControl. Open source to boot.

Adrian Hall

Because Developers are Awesome

Ratish Philip's Blog

Another adventure in code...

codematrix

Application Architecture at it's best

One Unicorn

Thoughts from one member of the Entity Framework team...

Rachel Lim's Blog

Simplifying programming into something I can understand

.NET Developer's Blog

Software Development, Flying and Life

The Flying Maverick

Software Development, Flying and Life

Hiking Photography

Beautiful photos of hiking and other outdoor adventures.

%d bloggers like this: