如何使用Powershell在Windows 10 Pro 1903中停止服务

时间:2019-10-22 17:00:36

标签: powershell

在Powershell中,我想停止服务:

Stop-Service -Name "StateRepository" -Force 

服务不会停止。我等了多久没什么关系。我已在Windows 10 Pro 1903中使用用户Administrator登录。没有任何错误。当我查看services.msc时,服务没有停止。

1 个答案:

答案 0 :(得分:0)

如果Stop-Service -Force无法正常工作,我不确定这是怎么回事。但是,您可以使用WMI / CIM获取服务的当前PID并以这种方式将其终止(请注意,这可能是不安全的操作):

$service = Get-CimInstance Win32_Service | Where-Object { $_.Name -eq "StateRepository" } | Select -First 1 Name, ProcessId
Write-Warning "Killing process $($service.ProcessId) for service $($service.Name)"
Stop-Process -Force $service.ProcessId

这就是说,最好总是调查一下为什么服务不会停止,因为这种技术更多的是不得已而为之。