Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

1 Answer 1

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

 
discard

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.