How to Create and Run Service in PowerShell

MD Aminul Islam Feb 15, 2024
How to Create and Run Service in PowerShell

Services are the background program that the system automatically generates for performing a specific task. But by using PowerShell, we can create a service manually.

In this article, we will see how we can manually create a service using PowerShell. Also, we make the topic easier by using an example and explanations.

Create and Run Service Using PowerShell

In our example below, we will create a simple service named TestService. The code for our example is below.

if ($args.count -eq 1) {
    $Service_Name = "TestService" # Setting the service name
    $Service_Root_Path = "D:\TestService\" # Setting the service root path
    $Service_Path = $Service_Root_Path + "\TestService.exe" # Setting the service path
    $Service_Description = "My Test Service" # Providing some description of the service
    $Service_Config = $Service_Root_Path + "\appsettings.Production.json" # Service configuration
    $InstanceConfig = "D:\InstanceConfig.config" # Instance configuration

   (Get-Content -Path $Service_Config -Raw) -Replace "1024", $args[0] | Set-Content -Path $Service_Config

    Copy-Item $InstanceConfig -Destination $Service_Root_Path

    # Checking if the service already exists. If it exists, delete it.
    if (Get-Service -Name $Service_Name -ErrorAction SilentlyContinue) {
        Stop-Service -Name $Service_Name # Stop the service
        sc.exe delete $Service_Name # Delete the service
    }

    New-Service -Name $Service_Name -BinaryPathName $Service_Path -Description $Service_Description
    # Modify service configuration settings
    [System.Environment]::SetEnvironmentVariable('ASPNETCORE_ENVIRONMENT', 'Production', [System EnvironmentVariableTarget]::Machine)
    Set-Service -Name $Service_Name -StartupType Automatic
    Start-Service -Name $Service_Name # Start the service
    Get-Service -Name $Service_Name
}
else
{
    # If the user didn't provide an InstanceId, return an error message.
    $Message = "Required to specify InstanceId argument"
    Write-Output $Message
}

The purpose of each line of the above PowerShell script is left as comments. After executing the above example code, you will get an output like the one below.

Execute the program by using the command below.

.\example.ps1 "YOUR_INSTANCE_ID"
Status   Name               DisplayName
------   ----               -----------
Stopped  TestService        TestService
Running  TestService        TestService

Test Service

To check if the service is included, go to the services of your system and find the service by name.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - PowerShell Service