Convenient Function:
(related to https://blog.kimconnect.com/powershell-kill-a-windows-service-forcefully)
# forceRestartService.ps1
# version 0.0.2
$serviceName='Spooler'
function forceRestartService($serviceName='Spooler'){
$processId=Get-WmiObject -Class Win32_Service -Filter "Name LIKE '$serviceName'"|Select-Object -ExpandProperty ProcessId
if($processId.count -eq 1){
try{
$taskkill=taskkill /f /pid $processid
write-host $taskkill
start-service $serviceName
$result=get-service $serviceName
if($result.Status -eq 'Running'){
write-host "$serviceName has successfully restarted" -foregroundcolor GREEN
return $true
}else{
write-host "$serviceName has NOT successfully restarted" -foregroundcolor RED
return $false
}
}catch{
write-warning $_
return $false
}
}else{
write-warning "$serviceName is not found on $env:computername"
return $null
}
}
forceRestartService $serviceName
# restartWindowsService.ps1
# version 0.0.1
$serviceName='spooler'
function restartWindowsService($serviceName){
$waitSeconds=40
$isValidProcess=try{[bool](get-service $serviceName -EA Stop)}catch{$false}
if($isValidProcess){
try{
$process=Start-Process -FilePath powershell.exe -ArgumentList "-Command Restart-Service $serviceName" -PassThru -NoNewWindow
$process|Wait-Process -Timeout $waitSeconds -ErrorAction Stop
write-host "$serviceName has been started successfully" -ForegroundColor Green
return $true
}catch{
write-warning $_
# $process|Stop-Process -Force
# $processId=(get-process $serviceName).Id
$processId=Get-WmiObject -Class Win32_Service -Filter "Name LIKE '$serviceName'"|Select-Object -ExpandProperty ProcessId
if($processId){
$processName=Get-Process -Id $processId
write-host "Program now forcefully kills PID $processId of process $processName belong to service $serviceName"
$null=$processId|%{taskkill /f /pid $_} # works more reliably than Stop-Process $processName -Force
Start-Service $serviceName
$started=$(try{if((get-service $serviceName).Status -eq 'Running'){$true}else{$false}}catch{$false})
if($started){
write-host "'$serviceName' status is now in 'Running' status" -ForegroundColor Green
return $true
}else{
write-warning "'$serviceName' status is in '$((get-service $serviceName).Status)' status" -foregroundcolor Red
return $false
}
}else{
write-warning "Service '$serviceName' PID not found."
return $false
}
return $false
}
}else{
write-warning "$serviceName doesn't match a valid Process Name on $env:computername"
return $null
}
}
restartWindowsService $serviceName
Symptom:
# Example error of Hyper-V Virtual Machine Management Service
Service 'Hyper-V Virtual Machine Management (vmms)' cannot be stopped due to the following error: Cannot stop vmms
service on computer '.'.
+ CategoryInfo : CloseError: (System.ServiceProcess.ServiceController:ServiceController) [Restart-Service], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Commands.RestartServiceCommand
+ PSComputerName : HYPERV05
Resolution:
# Kill the process id prior to attempting to start the service
$serviceName='vmms'
$processId=(get-process $serviceName).Id
$null=taskkill /f /pid $processId
Start-Service $serviceName
PS C:\Windows\system32> taskkill /f /pid $processId
SUCCESS: The process with PID 21404 has been terminated.
PS C:\Windows\system32> restart-service vmms
WARNING: Waiting for service 'Hyper-V Virtual Machine Management (vmms)' to stop...
WARNING: Waiting for service 'Hyper-V Virtual Machine Management (vmms)' to stop...
WARNING: Waiting for service 'Hyper-V Virtual Machine Management (vmms)' to stop...
WARNING: Waiting for service 'Hyper-V Virtual Machine Management (vmms)' to stop...
WARNING: Waiting for service 'Hyper-V Virtual Machine Management (vmms)' to stop...
Categories: