Skip to content

Instantly share code, notes, and snippets.

@unbaiat
Forked from yaplex/create-service-powershell.ps1
Created November 19, 2017 17:31
Show Gist options
  • Save unbaiat/985a2cd8a45737759b52babdf9eb96b1 to your computer and use it in GitHub Desktop.
Save unbaiat/985a2cd8a45737759b52babdf9eb96b1 to your computer and use it in GitHub Desktop.
$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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment