Powershell

How to Use the Restart-Service (Microsoft.PowerShell.Management) Cmdlet in PowerShell?

PowerShell’s “Restart-Service” cmdlet stops and then starts one or more specified services. It sends a message to the “Windows Service Controller” in order to stop service. Then, it sends a message to start the specified service. In case the service already stopped, it will start without displaying any error. Services can be specified to this cmdlet by their names and display names.

This guide will illustrate the usage of PowerShell’s “Restart-Service” cmdlet.

How to Use the Restart-Service (Microsoft.PowerShell.Management) Cmdlet in PowerShell?

First, write the stated cmdlet to utilize the “Restart-Service” cmdlet. Next, add the parameter “-Name” and provide it a service name to restart.

Let’s overview below-given illustrations to learn the usage of the stated cmdlet practically!

Example 1: Use the “Restart-Service” Cmdlet to Restart a Service
First, use the “Restart-Service” cmdlet. Then, write the “-Name” parameter and specify the service that you need to restart:

Restart-Service -Name WbioSrvc

Example 2: Use the “Restart-Service” Cmdlet to Start All Stopped Device Services
Execute the below-mentioned command to start all the stop device services:

Get-Service -Name "Dev*" | Where-Object {$_.Status -eq "Stopped"} | Restart-Service

Following the above command:

  • First, use the “Get-Service” cmdlet.
  • Next, add the parameter “-Name” and provide it with the initial letters of service.
  • Then, pipe it to the “Where-Object” cmdlet and specify the stated condition to get the stopped services.
  • Lastly, pipe it to the “Restart-Service” cmdlet:

Example 3: Use the “Restart-Service” Cmdlet to Start Services Using the Display Name Parameter Excluding One Service
Initially, use the “Restart-Service” cmdlet. Then, type the “-DisplayName” parameter and specify the service starting letters enclosed with the wildcard “*” character. Lastly, write the “-Exclude” parameter and assign the stated service name:

Restart-Service -DisplayName "sen*" -Exclude "sens"

That’s it! You have successfully learned the usage of the “Restart-Service” cmdlet.

Conclusion

The “Restart-Service” cmdlet is used to restart services in PowerShell. To do so, first, it sends a message to stop a specified service and then sends another message to start the service to the “Windows Controller Service”. This guide explained the “Restart-Service” cmdlet with the help of examples.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.