Restart Service - PowerShell


Overview

The Restart-Service command in PowerShell allows you to stop and then start a specified Windows service, ensuring the service is running smoothly. This command is particularly useful in troubleshooting service issues, restarting services after making configuration changes, or during system maintenance.

Syntax

Restart-Service [-Name] <ServiceName | ServiceDisplayName> [-Force] [-Confirm] [-PassThru] [-ThrottleLimit <int>] [-WhatIf] [-ErrorAction <ErrorAction>] [-ErrorVariable <string>] [-OutVariable <string>] [-OutBuffer <int>]

Options/Flags

  • -Name: Specifies the name of the service to restart.
  • -Force: Forces the restart of the service, even if it is in a critical state. Use with caution.
  • -Confirm: Prompts you for confirmation before restarting the service.
  • -PassThru: Returns an object representing the restarted service.
  • -ThrottleLimit: Sets the maximum number of services that can be restarted concurrently.
  • -WhatIf: Simulates the restart operation without actually performing it.
  • -ErrorAction: Specifies how errors are handled. Valid values include “Stop”, “Continue”, “SilentlyContinue”, and “Ignore”.
  • -ErrorVariable: Stores any errors encountered during the operation in a specified variable.
  • -OutVariable: Stores the output of the command in a specified variable.
  • -OutBuffer: Specifies the number of objects to buffer in memory before writing to the output variable.

Examples

Restart a specific service by name:

Restart-Service -Name "Windows Update"

Restart a service by display name:

Restart-Service -DisplayName "Windows Update Service"

Restart a service with confirmation:

Restart-Service -Confirm -Name "MSSQLSERVER"

Restart multiple services:

Restart-Service -Name (Get-Service -Name "MSSQL*", "IIS*")

Common Issues

Service is not found

  • Ensure that the service name or display name is spelled correctly and matches the name of an existing service.

Insufficient permissions

  • Restarting certain services requires elevated permissions. Run PowerShell as an administrator to resolve this issue.

Integration

Restart-Service can be combined with other commands for advanced tasks. For instance, you can use the Get-Service command to retrieve information about services, and then use Restart-Service to restart those services selectively.

  • Get-Service: Retrieves information about Windows services.
  • Set-Service: Configures settings for Windows services.
  • Stop-Service: Stops a specified Windows service.