Scripting Wife Uses PowerShell to Shut Down Computers

Scripting Wife Uses PowerShell to Shut Down Computers

  • Comments 9
  • Likes

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.

Image of menu

“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.

Image of command output

“It does not look as if it did anything,” she said.

“Do you see any errors?”

“No.”

“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.

Image of command output

“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.

Image of text file

“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.

Image of command output

“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.

Image of command output

“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 scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Doesn't this require you actually be a recognized admin on that other computer.

    For instance, if I'm on a LAN but not part of any Workgroup and thus and the sole admin on my computer, could someone who finds out my computer name on the LAN force my computer to shut down?

  • Hi, is possible to adapt this script so it can shutdown all computer accounts in a particular Active Directory OU? If so how?

    Thanks

  • RS you can either grab the computer names and do a for each loop through them or you can get the pc names and put it in a text file and loop through the file to shutdown each thats in the text file.

    visit my site at www.techjunkie.tv and you will get the idea of what has to happen, also try out this book powershell in action it is very sweet

    www.ScottAlvarino.com

  • @Vordreller yes it requires admin rights to shut down a computer. If you are in a workgroup, the "someone" would need admin rights on your computer. This particular method relies upon WMI, and therefore specifically by default WMI requires admin rights for remote shutdown. It is possible to configure WMI to allow a different group to perform this action, but this is not default.

    @RS You can use DSQuery, or [adsisearcher] or the Windows 2008 R2 AD cmdlets, Quest AD cmdlets, PSCX cmdlets or even several other methods. Use the PowerShell / AD tags from the right bar to find specifics. I have written dozens of articles about this type of thing.

  • The only problem with stop-computer or restart-computer cmdlet is that, it can not accept a comment. This helps us to track back to understand for what purpose a computer is shutdown or restarted. I authored a script to fill this gap.

    techibee.com/.../1362

  • Hey ED,

    Thanks for this guide, this is my first dabbling with PS.

  • good stuff

  • I found a create article - it is for VBS scripting.  It shuts down a computer if the computer is (on, no user logged in or a computer is logged in an the system is idle)

    Really cool script.

    www.webandtechguy.com/.../97-vbs-shutdown-remote-computer-ad-check-for-idle

    Dave

  • Can also try the following:

    Get-Content C:\fso\Desktops.txt | foreach-object { Stop-Computer -cn $_ –force }