Restart a service on remote Computers, using the simple Powershell code one can restart the service on any remote computer or multiple computers with progress and computer details.
An IT administrator must come across a situation or need to restart a service on many remote machines, it may be on servers or desktop workstations, we generally restart service after a new upgrade, new software installation or a configuration change on the machines.
In Powershell, we have a command Restart-Service available to restart any service.
Syntax:
Powershell Restart-Service Please find the all parameter information explained at the bottom of the article.
Examples:
Let’s restart “Windows Update” service on local machine.
Restart-Service -Name wuauserv
Example 2
Restart-Service on a remote machine
we have a remote machine Win10VDI99, let’s restart the same service.
Invoke-Command -ComputerName Win10VDI99 -ScriptBlock {Restart-Service -name wuauserv}
Example 3
Restart-Service on Multiple remote machines
When you have a requirement to restart a service on multiple remote machines, then we need to write the script this way.
We need to create a file for storing all the servers or multiple machines list, I named it as Allserver.txt for and saved it in my local machine c:\temp\Allserver.txt
#remote machines list $servers = Get-Content -Path "C:\temp\AllServer.txt" foreach ($server in $servers) { Write-Host "Currently the script is restarting the serivce on" $server Invoke-Command -ComputerName $server -ScriptBlock {Restart-Service -name wuauserv} }
Note: In the above examples script I have taken the service name as wuauserv, it’s windows update service, you can replace with your required service name.
Parameters of Restart-Service
-Force
This parameter forces the command to restart a service without asking the user confirmation.
Restart-Service -Name wuauserv -force
-Name
This parameter is used for specifying the name fo the service that you want to restart.
-Confirm
This parameter prompts user confirmation before it restarts the service.
Restart-Service -Name wuauserv -Confirm
-DisplayName
This parameter used for specifying the display name of the service that you need to restart.
Restart-Service -DisplayName Windows Update
–Exclude
This parameter excludes the services that are not required to restart, wildcard characters are permitted.
Restart-Service -DisplayName “win” -Exclude “net logon”
-Include
Restart-Service -DisplayName “win” -Include “net logon”
-PassThru
This parameter returns an object that represents the service.
-WhatIf
This parameter shows what happens when you execute a command, I strongly recommend testing this before you start multiple services at once.
Thank you for reading this article, if you have any questions please let us know.
Thank you for visiting my site, for any scripts in these articles you are testing please make sure you have tested this script in our lower environment before you run in production.
Leave a Reply Cancel reply