How to delete a Windows service in PowerShell
By FoxLearn 1/2/2025 6:48:45 AM 194
Check if a Windows service exists and delete it in PowerShell
Press Win + X
and select Windows PowerShell (Admin) from the menu.
Since there is no `Remove-Service` cmdlet in PowerShell versions prior to 6.0, you can use WMI (Windows Management Instrumentation) or other tools like `sc.exe` to remove a service.
In PowerShell 6.0 and later, `Remove-Service` is available for directly removing services.
For example:
$service = Get-WmiObject -Class Win32_Service -Filter "Name='servicename'" $service.delete()
To check if a Windows service exists and delete it in PowerShell.
$serviceName = "YourServiceName" # Check if the service exists $service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue if ($service) { # If the service exists, stop it first Stop-Service -Name $serviceName -Force # Then, delete the service using WMI $wmiService = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" $wmiService.Delete() Write-Host "Service $serviceName has been deleted." } else { Write-Host "Service $serviceName does not exist." }
Or with the sc.exe
tool:
sc.exe delete ServiceName
Make sure you use the service name, not the display name. You can find the service name by going to services.msc and checking the properties of the service.
You can run the following command to stop the service before you delete it.
Stop-Service -Name "ServiceName"
If the service is already stopped or if you don't need to stop it first, you can skip this step.
To delete the service, use the Remove-Service command.
Remove-Service -Name "ServiceName"
Removing a Windows Service by Display Name
To remove a service named 'TestService'. I use the `Get-Service` cmdlet to retrieve the service object by its display name. The object is then passed through the pipeline (`|`) to the `Remove-Service` cmdlet, which deletes the service.
Get-Service -DisplayName "Test Service" | Remove-Service
To check if a Windows service exists in PowerShell, you can use the `Get-Service` cmdlet to query the service by its name.
Get-Service "ServiceName"
If it’s deleted, you should get an error saying the service cannot be found.
Deleting a service requires administrative privileges, which is why you need to run PowerShell as an administrator.
- How to execute PowerShell script in C#
- How to display GUI Message Boxes in PowerShell
- How to sign a powershell script
- How to run powershell script using task scheduler
- How to run powershell script from cmd
- How to run powershell commands in C#
- How to download file from url in PowerShell
- How to use string interpolation in PowerShell