mrhobbeys
mrhobbeys

Reputation: 13

Disabling Services via Powershell for computer management

My code here should disable services in the list. But I get an error that

method invocation failed because system string doesn't contain a a method named ChangeStartMode

(gwmi win32_service -filter "name = 'SharedAccess' Or name = 'cx2Svc' OR name = 'NetTcpPortSharing' OR name = 'RemoteAccess' OR name = 'AxInstSV' OR name = 'SensrSvc' OR name = 'ALG' OR name = 'AppMgmt' OR name = 'BDESVC' OR name = 'bthserv' OR name = 'PeerDistSvc' OR name = 'CertPropSvc' OR name = 'VaultSvc' OR name = 'DPS' OR name = 'WdiServiceHost' OR name = 'WdiSystemHost' OR name = 'TrkWks' OR name = 'EFS' OR name = 'Fax' OR name = 'fdPHost' OR name = 'FDResPub' OR name = 'hkmsvc' OR name = 'hidserv' OR name = 'UI0Detect' OR name = 'iphlpsvc' OR name = 'lltdsvc' OR name = 'MSiSCSI' OR name = 'Netlogon' OR name = 'napagent' OR name = 'CscService' OR name = 'WPCSvc' OR name = 'PNRPsvc' OR name = 'p2psvc' OR name = 'p2pimsvc' OR name = 'IPBusEnum' OR name = 'PNRPAutoReg' OR name = 'WPDBusEnum' OR name = 'wercplsupport' OR name = 'PcaSvc'").ChangeStartMode("Disabled")

I have tried changing the quoting and tried making it one service. I also tried using ChangeServiceStart and ChangeServiceStartType which I found online for other people's scripts but none of those worked. I have also tried this on several computers in powershell v1.0 v2.0 and one with WMI 3.0

Upvotes: 1

Views: 615

Answers (1)

Andy Arismendi
Andy Arismendi

Reputation: 52609

You'll need to use foreach or ForEach-Object to call that method for each result returned -

Simplified example using ForEach-Object -

(gwmi win32_service -filter "name = 'SharedAccess' Or name = 'cx2Svc') | 
    ForEach-Object {$_.ChangeStartMode("Disabled")}

You could also use the service cmdlets for this activity -

Get-Service -Name SharedAccess, cx2Svc -EA 0 | 
   Set-Service -StartupType Disabled

Upvotes: 1

Related Questions