{"id":15041,"date":"2011-04-01T00:01:00","date_gmt":"2011-04-01T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/04\/01\/scripting-wife-uses-powershell-to-shut-down-computers\/"},"modified":"2011-04-01T00:01:00","modified_gmt":"2011-04-01T00:01:00","slug":"scripting-wife-uses-powershell-to-shut-down-computers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/scripting-wife-uses-powershell-to-shut-down-computers\/","title":{"rendered":"Scripting Wife Uses PowerShell to Shut Down Computers"},"content":{"rendered":"<p><strong>Summary<\/strong>: The Scripting Wife learns how to use Windows PowerShell to shut down remote computers.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, here. It is raining outside and every so often a window-shaking thunder clap erupts outside, jarring the teeth of everyone inside our house in Charlotte, North Carolina. Because I know that the Scripting Wife enjoys thunder and lightning as much as I do, I expect to see her arrive in my office at any minute. Wait a minute&hellip;I think I hear her. She is not even moving in stealth mode. <\/p>\n<p>&ldquo;Did you hear that?&rdquo; she asked with a sense of excitement.<\/p>\n<p>&ldquo;I am only deaf in one ear. The other one works just fine. Even if I did not hear it, I would have felt the last one. It was cool,&rdquo; I replied.<\/p>\n<p>&ldquo;Cool, my foot. It is downright groovy,&rdquo; she said. &ldquo;But I do think we need to shut down all of our computers.&rdquo;<\/p>\n<p>&ldquo;They are all plugged into UPSs,&rdquo; I said calmly.<\/p>\n<p>&ldquo;Yeah, but they only last so long. I don&rsquo;t want my computer crashing because the lights were out longer than the UPS will hold,&rdquo; she exclaimed. &ldquo;Besides, you know that around here, it does not take a thunderstorm to knock out the electricity&mdash;a small breeze will suffice.&rdquo;<\/p>\n<p>I guess perhaps she has a point. So I decided to turn it into a teachable moment. <\/p>\n<p>&ldquo;We will need to use Windows PowerShell to shut down all of our computers,&rdquo; I said.<\/p>\n<p>&ldquo;You would use Windows PowerShell to make a peanut butter and jelly sandwich,&rdquo; she said.<\/p>\n<p>&ldquo;I would if I could find a sandwich provider,&rdquo; I said. &ldquo;I have a great name for the cmdlet: Create-Sandwich.&rdquo;<\/p>\n<p>&ldquo;Droll. Very Droll.&rdquo;<\/p>\n<p>&ldquo;Let&rsquo;s get started,&rdquo; I said. &ldquo;You can log on to that computer over there. Make sure you start the Windows PowerShell console with administrative rights.&rdquo;<\/p>\n<p>The Scripting Wife right-clicked the Windows PowerShell icon, and selected <b>Run as Administrator<\/b> from the <b>Tasks<\/b><i> <\/i>menu. This menu is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0508.HSG-4-1-11-01_347F34C3.jpg\"><img decoding=\"async\" height=\"290\" width=\"304\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0116.HSG-4-1-11-01_thumb_1C2C1AA8.jpg\" alt=\"Image of menu\" border=\"0\" title=\"Image of menu\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>&ldquo;To shut down your computers, you can use the <b>Stop-Computer<\/b> cmdlet. The good thing is that it allows you to specify the list of computer names as an array to the <i>computername <\/i>parameter. There is also a <i>force <\/i>parameter that you can use if you are concerned about shutting down the computers immediately,&rdquo; I said.<\/p>\n<p>The Scripting Wife thought for a second, and then typed the following command to shut down two of the computers that are on the network. The command she typed is shown here. <\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span lang=\"EN\" style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">Stop-Computer -ComputerName &#8220;Vista&#8221;,&#8221;Teresa-Kitchen&#8221; -Force<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The command and its associated output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3666.HSG-4-1-11-02_4E434525.jpg\"><img decoding=\"async\" height=\"176\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2021.HSG-4-1-11-02_thumb_442EA3FA.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>&ldquo;It does not look as if it did anything,&rdquo; she said.<\/p>\n<p>&ldquo;Do you see any errors?&rdquo;<\/p>\n<p>&ldquo;No.&rdquo;<\/p>\n<p>&ldquo;Then it worked just fine. Why don&rsquo;t you use the Up arrow and try to shut down the computers a second time?&rdquo; I suggested.<\/p>\n<p>The Scripting Wife re-called the previous command by using the Up arrow, and then she pressed ENTER. <\/p>\n<p>&ldquo;It looks like it is hung,&rdquo; she complained.<\/p>\n<p>&ldquo;Wait a little bit. It is attempting to connect, and it takes a while to time out. You can press ENTER a couple times if it will make you feel better,&rdquo; I suggested.<\/p>\n<p>The Scripting Wife waited for a couple minutes, and then began hitting the Enter key several times. The error returned as &ldquo;The RPC server is unavailable.&rdquo; Then she received several blank lines from where she had hit ENTER. The error message is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4657.HSG-4-1-11-03_1AD75BFC.jpg\"><img decoding=\"async\" height=\"326\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7416.HSG-4-1-11-03_thumb_65EA56BE.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>&ldquo;OK. Now go turn those two computers back on because I want to show you something cool,&rdquo; I said.<\/p>\n<p>The Scripting Wife soon returned from turning on the kitchen computer and the old Vista computer that resides in the TV room. <\/p>\n<p>&ldquo;What is so cool that you needed to send me all over the house turning on computers that I had just turned off?&rdquo; she asked.<\/p>\n<p>&ldquo;Open Notepad and add the Vista, Teresa-Kitchen, and win7-c1 computer to it,&rdquo; I instructed.<\/p>\n<p>It took just a little bit of typing, and she had produced the text file that is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3122.HSG-4-1-11-04_3C930EC0.jpg\"><img decoding=\"async\" height=\"264\" width=\"404\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3108.HSG-4-1-11-04_thumb_5305AD47.jpg\" alt=\"Image of text file\" border=\"0\" title=\"Image of text file\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>&ldquo;Now I want you to use the <b>Get-Content<\/b> cmdlet to read the content of the Desktops.txt file and supply it to the <b>Stop-Computer<\/b> cmdlet. Remember that the <i>computername<\/i> parameter will accept an array,&rdquo; I said.<\/p>\n<p>The Scripting Wife thought for a second, and then used the <b>Get-Content<\/b> cmdlet to read the content of the text file. She then piped it to the <b>Stop-Computer<\/b> cmdlet. The command generated an error. She then tried to use the <b>$_ automatic<\/b> variable with the <i>computername<\/i> parameter (she used the <b>CN<\/b> alias). That command also generated an error. Finally, she tried the <b>Get-Content <\/b>cmdlet by itself to make sure that she was at least finding the text file. The three commands and their associated errors are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2553.HSG-4-1-11-05_69784BCE.jpg\"><img decoding=\"async\" height=\"406\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0312.HSG-4-1-11-05_thumb_33B2E0A7.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>&ldquo;It doesn&rsquo;t work,&rdquo; she complained.<\/p>\n<p>&ldquo;You are trying too hard. All you need to do is to use parentheses around the <b>Get-Content<\/b> command in the position of the <i>computername<\/i> parameter,&rdquo; I said. &ldquo;Now, try it again.&rdquo;<\/p>\n<p>The Scripting Wife thought for a second, and then she highlighted the <b>Get-Content <\/b>command from her previous command and pasted it into the <i>computername<\/i> parameter. Her command is shown here.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span lang=\"EN\" style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">Stop-Computer -cn (Get-Content C:\\fso\\Desktops.txt) &ndash;force<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The command and its associated output (which is none) is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4064.HSG-4-1-11-06_01F7C352.jpg\"><img decoding=\"async\" height=\"109\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4075.HSG-4-1-11-06_thumb_75C6235D.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>&ldquo;That is all you need to do,&rdquo; I said.<\/p>\n<p>&ldquo;That is pretty neat,&rdquo; she admitted.<\/p>\n<p>&ldquo;Well, all the thunder and lightning has pretty much quit, so I guess you should go turn those computers back on,&rdquo; I said.<\/p>\n<p>&ldquo;I wish I could write a script to start the computers,&rdquo; she whined.<\/p>\n<p>&ldquo;Actually, you can do that by using WinRM. But that is another story,&rdquo; I said.<\/p>\n<p>&ldquo;OK. Sounds like a plan. I will see you later, gater,&rdquo; she said as she headed out of the office. <\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: The Scripting Wife learns how to use Windows PowerShell to shut down remote computers. Microsoft Scripting Guy, Ed Wilson, here. It is raining outside and every so often a window-shaking thunder clap erupts outside, jarring the teeth of everyone inside our house in Charlotte, North Carolina. Because I know that the Scripting Wife enjoys [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[16,47,3,74,45],"class_list":["post-15041","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-general-management-tasks","tag-scripting-guy","tag-scripting-wife","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: The Scripting Wife learns how to use Windows PowerShell to shut down remote computers. Microsoft Scripting Guy, Ed Wilson, here. It is raining outside and every so often a window-shaking thunder clap erupts outside, jarring the teeth of everyone inside our house in Charlotte, North Carolina. Because I know that the Scripting Wife enjoys [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=15041"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15041\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=15041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}