DEV Community

Daniel Leinweber
Daniel Leinweber

Posted on

Create a Windows Service from the Command Line

During my day-to-day job we create a lot of Windows Services, based on the .NET Worker Service. These need to be installed on server systems. Instead of installing them manually I use the following PowerShell script.

You just need to fill in the variables for $serviceName, $executablePath, $userName, $password and $description.

$serviceName = "My_Service_Name"
$executablePath = "C:\temp\Path_to_my_service.exe"
$userName = "My_User_Name"
$password = "My_Password"
$description = "My service description"

sc.exe stop $serviceName
sc.exe delete $serviceName
sc.exe create $serviceName binpath= $executablePath start= delyed-auto obj= $userName password= $password
sc.exe description $serviceName $description
Enter fullscreen mode Exit fullscreen mode

Note: Please note that there are spaces between the sc.exe parameters equal sign and their values. This is on purpose.

Top comments (0)