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 thunder and lightning as much as I do, I expect to see her arrive in my office at any minute. Wait a minute…I think I hear her. She is not even moving in stealth mode.
“Did you hear that?” she asked with a sense of excitement.
“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,” I replied.
“Cool, my foot. It is downright groovy,” she said. “But I do think we need to shut down all of our computers.”
“They are all plugged into UPSs,” I said calmly.
“Yeah, but they only last so long. I don’t want my computer crashing because the lights were out longer than the UPS will hold,” she exclaimed. “Besides, you know that around here, it does not take a thunderstorm to knock out the electricity—a small breeze will suffice.”
I guess perhaps she has a point. So I decided to turn it into a teachable moment.
“We will need to use Windows PowerShell to shut down all of our computers,” I said.
“You would use Windows PowerShell to make a peanut butter and jelly sandwich,” she said.
“I would if I could find a sandwich provider,” I said. “I have a great name for the cmdlet: Create-Sandwich.”
“Droll. Very Droll.”
“Let’s get started,” I said. “You can log on to that computer over there. Make sure you start the Windows PowerShell console with administrative rights.”
The Scripting Wife right-clicked the Windows PowerShell icon, and selected Run as Administrator from the Tasks menu. This menu is shown in the following image.
“To shut down your computers, you can use the Stop-Computer cmdlet. The good thing is that it allows you to specify the list of computer names as an array to the computername parameter. There is also a force parameter that you can use if you are concerned about shutting down the computers immediately,” I said.
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.
Stop-Computer -ComputerName “Vista”,”Teresa-Kitchen” -Force
The command and its associated output are shown here.
“It does not look as if it did anything,” she said.
“Do you see any errors?”
“Then it worked just fine. Why don’t you use the Up arrow and try to shut down the computers a second time?” I suggested.
The Scripting Wife re-called the previous command by using the Up arrow, and then she pressed ENTER.
“It looks like it is hung,” she complained.
“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,” I suggested.
The Scripting Wife waited for a couple minutes, and then began hitting the Enter key several times. The error returned as “The RPC server is unavailable.” Then she received several blank lines from where she had hit ENTER. The error message is shown in the following image.
“OK. Now go turn those two computers back on because I want to show you something cool,” I said.
The Scripting Wife soon returned from turning on the kitchen computer and the old Vista computer that resides in the TV room.
“What is so cool that you needed to send me all over the house turning on computers that I had just turned off?” she asked.
“Open Notepad and add the Vista, Teresa-Kitchen, and win7-c1 computer to it,” I instructed.
It took just a little bit of typing, and she had produced the text file that is shown here.
“Now I want you to use the Get-Content cmdlet to read the content of the Desktops.txt file and supply it to the Stop-Computer cmdlet. Remember that the computername parameter will accept an array,” I said.
The Scripting Wife thought for a second, and then used the Get-Content cmdlet to read the content of the text file. She then piped it to the Stop-Computer cmdlet. The command generated an error. She then tried to use the $_ automatic variable with the computername parameter (she used the CN alias). That command also generated an error. Finally, she tried the Get-Content 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.
“It doesn’t work,” she complained.
“You are trying too hard. All you need to do is to use parentheses around the Get-Content command in the position of the computername parameter,” I said. “Now, try it again.”
The Scripting Wife thought for a second, and then she highlighted the Get-Content command from her previous command and pasted it into the computername parameter. Her command is shown here.
Stop-Computer -cn (Get-Content C:\fso\Desktops.txt) –force
The command and its associated output (which is none) is shown here.
“That is all you need to do,” I said.
“That is pretty neat,” she admitted.
“Well, all the thunder and lightning has pretty much quit, so I guess you should go turn those computers back on,” I said.
“I wish I could write a script to start the computers,” she whined.
“Actually, you can do that by using WinRM. But that is another story,” I said.
“OK. Sounds like a plan. I will see you later, gater,” she said as she headed out of the office.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy