{"id":2378,"date":"2014-01-02T02:43:42","date_gmt":"2014-01-02T02:43:42","guid":{"rendered":"https:\/\/pipe2text.com\/?page_id=2378"},"modified":"2014-01-03T19:12:07","modified_gmt":"2014-01-03T19:12:07","slug":"using-powershell-to-restart-a-service-on-multiple-remote-machines","status":"publish","type":"page","link":"https:\/\/pipe2text.com\/?page_id=2378","title":{"rendered":"Using PowerShell to Restart a Service on Multiple Remote Machines"},"content":{"rendered":"<p>Hi All,<\/p>\n<p>There may be times when you will need to restart a service on multiple servers whether it be some sort of configuration change or update. To save yourself from having to either logon to each server or connect one by one using remote tools, you can simply use PowerShell to restart the service. First I will start by demonstrating how to restart a service on one remote machine and then I will show how to restart a service on multiple remote computers. You will need to open up Windows Remote Management WinRM on the remote computers before you run PowerShell commands against them remotely (Windows 2012 has this\u00a0enabled by default). Configuring WinRM is outside the scope of this article. This article assumes that WinRM is already configured. This article also assumes the remote service being restarted does NOT have any dependent services.<\/p>\n<p>Before I begin I would like to show you how to get the name of a service and services information in general using PowerShell. All you need to do is type the following command to list all the services on a machine as well as the Status, Name and DisplayName. In the example below, I will get the services for a server named PRINTSERVER. Open PowerShell and enter the following command:<\/p>\n<p>Get-Service -ComputerName PRINTSERVER<\/p>\n<p>As mentioned earlier, this article assumes that the remote service you are restarting does not have other services dependent on it. To see if a service has dependent services on the remote machine you can use the \u2013DependentServices parameter. Type the following to find out if there are dependent services for the spooler service on a server named PRINTSERVER:<\/p>\n<p>Get-Service -ComputerName PRINTSERVER -name spooler \u2013DependentServices<\/p>\n<p>If the above command returns no results you will know that there are no dependent services.<\/p>\n<p>Now that you have a list of the services, I will demonstrate how to restart the print spooler service on a single machine named \u201cPRINTSERVER\u201d in the example below. Replace PRINTSERVER with the name of the server or workstation for which you would like to restart the service. Also replace \u201cspooler\u201d with the name of the service you would like to restart. Make sure you are running PowerShell with an account that has access to restart services on\u00a0the remote computer. Since the \u201cRestart-Service\u201d cmdlet does not have a parameter to manage services on remote machines, we use the \u201cInvoke-Command\u201d cmdlet. Run the following command:<\/p>\n<p>Invoke-Command -Computername <em>PRINTSERVER <\/em>{Restart-Service <em>spooler<\/em>}<\/p>\n<p>Next I want to show you how to restart services on multiple remote machines. First we will need to create a text file with the names of the machines for which\u00a0we would like to restart the specified services. Open Notepad and type a list of servers line by line that you want the \u201cRestart-Service\u201d command to be executed against as shown below. Save the text file. In this example I am saving the file as Servers.txt.<\/p>\n<p><img decoding=\"async\" style=\"display: inline;\" alt=\"\" src=\"https:\/\/pipe2text.com\/wp-content\/plugins..\/..\/uploads\/media\/using-powershell-to-restart-a-service-on-multiple-remote-machines\/image1.png\" \/><\/p>\n<p>Now we will execute the 2 lines below to restart the spooler service on servers PRINTSERV1 and PRINTSERV2.<\/p>\n<p><span style=\"font-size: medium;\">$computers = Get-Content \u201cc:\\Scripts\\servers.txt\u201d<br \/>\nInvoke-Command -ComputerName $computers {Restart-Service Spooler}<br \/>\n<\/span><\/p>\n<p>If you check the system log on the remote\u00a0machines you will see that the service you specified was restarted.<\/p>\n<p>I hope this helps. It\u2019s always a good idea to test this and other commands in a lab scenario first. If you have any questions or feedback, please leave a comment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi All, There may be times when you will need to restart a service on multiple servers whether it be some sort of configuration change or update. To save yourself from having to either logon to each server or connect &hellip; <a href=\"https:\/\/pipe2text.com\/?page_id=2378\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"class_list":["post-2378","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/2378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pipe2text.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2378"}],"version-history":[{"count":20,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/2378\/revisions"}],"predecessor-version":[{"id":2425,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/2378\/revisions\/2425"}],"wp:attachment":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}