小编典典

检查 Windows 服务是否存在并在 PowerShell 中删除

all

我目前正在编写一个安装许多 Windows 服务的部署脚本。

服务名称是版本化的,因此我想在安装新服务时删除以前的 Windows 服务版本。

我怎样才能最好地在 PowerShell 中做到这一点?


阅读 63

收藏
2022-08-27

共1个答案

小编典典

您可以为此使用 WMI 或其他工具,因为在 Powershell 6.0 之前没有Remove-Servicecmdlet(请参阅 Remove-
Service doc)

例如:

$service = Get-WmiObject -Class Win32_Service -Filter "Name='servicename'"
$service.delete()

或使用sc.exe工具:

sc.exe delete ServiceName

最后,如果您确实有权访问 PowerShell 6.0:

Remove-Service -Name ServiceName
2022-08-27