Script Library
Reviewed PowerShell Automation
Restart a Selected Windows Service
Safely previews and restarts one explicitly named Windows service after validating its current state.
AdministrationWindowsPowerShellModerate riskAdministrator requiredDry-run documented
What it does
- Verifies the selected service exists
- Displays its current status and display name
- Restarts only the selected service after confirmation
Required inputs
-ServiceName RequiredWindows service name—not the display name—confirmed with Get-Service.
What it changes
- Temporarily stops and starts the selected service
- Applications or features that depend on the service may be interrupted
Complete source
Read the entire script before copying or downloading it.
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="High")]
param([Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][string]$ServiceName)
$ErrorActionPreference = "Stop"
$service = Get-Service -Name $ServiceName -ErrorAction Stop
$service | Select-Object Status, Name, DisplayName | Format-Table -AutoSize
if ($PSCmdlet.ShouldProcess("$($service.DisplayName) [$($service.Name)]", "Restart Windows service")) {
Restart-Service -Name $service.Name -ErrorAction Stop
$service.WaitForStatus("Running", [TimeSpan]::FromSeconds(30))
Get-Service -Name $service.Name | Select-Object Status, Name, DisplayName | Format-Table -AutoSize
}