PowerShell – Stop process before Service

Just another Tech site

PowerShell – Stop process before Service

##  Stop process before Service

# Define the process name and service name
$processName = "YourProcessName"
$serviceName = "YourServiceName"

# Stop the process
$process = Get-Process -Name $processName -ErrorAction SilentlyContinue
if ($process) {
    Stop-Process -Name $processName -Force
    Write-Output "Process '$processName' has been stopped."
} else {
    Write-Output "Process '$processName' is not running."
}

# Stop the service
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue
if ($service.Status -eq 'Running') {
    Stop-Service -Name $serviceName -Force
    Write-Output "Service '$serviceName' has been stopped."
} else {
    Write-Output "Service '$serviceName' is not running."
}