How to restart a service using PowerShell


Powershell is a command-line interpreter. It uses commands such as Start-Service, Get-Service, Restart-Service, and Stop-Service, for performing different types of tasks. These commands are called “cmdlets”.

Restarting a service is a typical job in system administration. Powershell also helps to do the specified operation on remote computers, such as starting, stopping, or restarting services.

In this post, we will learn how to restart a service using PowerShell.

How does the Restart-Service cmdlet work in PowerShell?

In the “Restart-Service” process, the cmdlet first sends the message to the Windows Service Controller to stop the service and then start it again. If the service is already stopped, it just starts without sending any warning.

For restarting the service, we will use the below-given syntax.

Syntax
The syntax of the Restart-Service command is:

Restart-Service –<parameters>

Here, the added “<parameters>” tells the compiler about the action that Restart-Service needs to perform. Some of those parameters will be discussed here:

Syntax Description
Restart-Service -Name <string[]> The parameter “Name” is an identifier that shows the name of the services.
Restart-Service -DisplayName <string[]> The parameter “DisplayName” is also an identifier that displays the complete name of the services.
Restart-Service [-PassThru] With the “-PassThru” parameter, Restart-Service is forced to wait until the service has started and has displayed its operating status.
Restart-Service [-Force] Similarly, the “-Force” parameter tells the compiler to restart the specified service forcefully.
Restart-Service [-Include <string[]>] The “-Include” parameter tells which service is included in the restarting process.
Restart-Service [-Exclude <string[]>] The “-Exclude” parameter indicates which service is excluded in the restarting process

How to restart a service using PowerShell?

This section will demonstrate some examples of using “Restart-Service” in Powershell.

Example 1
First of all, we will get the list of services by using the “Get-Service” cmdlet. This command will print out the list of services with their “Status”, “Name”, and “DisplayName”:

Now, we will restart the last two services, “XboxNetApiSvc” and “ZapyaService”; one is stopped, and the other one is in a running state:

When the “ZapyaService” is restarted, it sends a message to the Window Service Controller to stop it and start it again without any warning:

> Restart-Service ZapyaService

In case of restarting the “XboxNetApiSvc” service, only a message will be sent to the Window Service Controller to restart it, as it was already in the “Stopped” state:

> Restart-Service XboxNetApiSvc

Now, again, check the status of the services by using the “Get-Service” cmdlet:

As you can see, both “ZapyaService” and “XboxNetApiSvc” services are currently in the “Running” state:

Example 2
Now, let’s see how to use Restart-Service with the mentioned parameters. Here, in the following example, we will utilize the “-Exclude” parameter to restart all Xbox services except the “Xbox Live Networking Service”.

Before executing the Restart-Service, firstly, we will check the current status of the Xbox services using the “Get-Service” command:

As you can see, there are four services related to “Xbox” two are in the stopped state, and the remaining are in the running state:

Now, type out the following command in PowerShell to restart Xbox services except “Xbox Live Networking Service” as it is already in running state:

> Restart-Service -DisplayName «xbox*» -Exclude » Xbox Live Networking Service»

After restarting, we will check the status of the restarted services by using cmdlet “Get-Service”:

The given output indicates that now other three Xbox services are also in the “Running” state:

We have provided the essential information related to restarting services using PowerShell.

Conclusion

To restart a service in PowerShell, you can utilize the “Restart-Service” command. The syntax of the Restart-Service command is given as: “Restart-Service -<parameters>”, where the added “<parameters>” tells the compiler about the action that Restart-Service needs to perform. This command can be used to restart an already stopped or running service. In this post, we learned how to restart a service using PowerShell with examples.



Source link