How to find, stop and disable a Windows service using Powershell
You need to use Set-Service
to set the startup type, as outlined in your question:
Set-Service -StartupType Disabled $svc_name
If you want to do it in "one line", you can use the -PassThru
argument on Stop-Service
to return the service object which can then be sent down the pipeline (you also don't need a Where-Object
clause, Get-Service
can filter on service name as well):
Get-Service -ComputerName $ip $svc_name | Stop-Service -PassThru | Set-Service -StartupType Disabled
You had this close in your original question, but it didn't work because you didn't use the-PassThru
parameter on Stop-Service
. As a note, many cmdlets that don't return an object by default do include a -PassThru
parameter to return an object that can further processed if necessary, this isn't limited to Stop-Service
by any means.
Related videos on Youtube
Rafiq
Updated on June 04, 2022Comments
-
Rafiq almost 2 years
I am trying to find a service, stop it and then disable it remotely using Powershell. It can find and stop but cannot disable. For disabling, I have to run the
Set-Service
command separately. Can it be done in one line?The following code-snippet will stop the Print Spooler service, but will not disable it:
$ip = "10.10.10.10" $svc_name = "Spooler" get-service -ComputerName $ip | Where-Object {$_.Name -eq $svc_name} | Stop-Service | Set-Service -StartupType Disabled
The following code-snippet will stop and disable the Print Spooler service:
$ip = "10.10.10.10" $svc_name = "Spooler" get-service -ComputerName $ip | Where-Object {$_.Name -eq $svc_name} | Stop-Service Set-Service $svc_name -StartupType Disabled
Powershell version is
5.1.14393.2969
.Edit: The following line will also find and disable. So, it looks like I can give two instructions with pipeline.
get-service -ComputerName $ip | Where-Object {$_.Name -eq $svc_name} | Set-Service -StartupType Disabled
-
codewario about 2 yearsThis answer is bloated for what the OP was asking for. This should probably be trimmed, have the commented out code removed, and the relevant pieces to the question explained.