Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using powershell 1.0 and I need to install a service on a remote machine and first uninstall it if it exists.

This is my script I have that installs the service, however, I seem unable to uninstall the service. I have tried installutil however the service path is a network path which installutil, throws errors over.

I'm a complete newbie with powershell, however, I'm sure there must be a better and cleaner way of approaching this.

$class = "Win32_Service"
$method = "Create"
$mc = [wmiclass]"\\DUMMYServer\ROOT\CIMV2:$class"
$inparams = $mc.PSBase.GetMethodParameters($method)
$inparams.DesktopInteract = $false
$inparams.DisplayName = "DummyService"
$inparams.ErrorControl = 0
$inparams.LoadOrderGroup = $null
$inparams.LoadOrderGroupDependencies = $null
$inparams.Name = "DummyMessageService"
$inparams.PathName = '\\DummyServer\c$\Applications\DummyMessageWindowsService\DummyWindowsService.exe'
$inparams.ServiceDependencies = $null
$inparams.ServiceType = 16
$inparams.StartMode = "Automatic"
$inparams.StartName = $null # will start as localsystem builtin if null
$inparams.StartPassword = $null

$result = $mc.PSBase.InvokeMethod($method,$inparams,$null)
$result | Format-List
share|improve this question

try using wmi rather than all the fancy params.... Something like this (untested)

(gwmi win32_service -filter "name='yourservicename'").delete()

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.