Skip to content

Instantly share code, notes, and snippets.

@yaplex
Created June 20, 2017 19:55
Show Gist options
  • Save yaplex/a58863e02ace1d5c7ddd32f2e896894b to your computer and use it in GitHub Desktop.
Save yaplex/a58863e02ace1d5c7ddd32f2e896894b 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"
# current directory
$directorypath = Split-Path $MyInvocation.MyCommand.Path
$secpasswd = ConvertTo-SecureString "MyP@$$w0rd" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential (".\MyUserName", $secpasswd)
# you need hardcode application name anyway
$binaryPath = $directorypath + "\ApplicationName.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