Script to shutdown Windows 7 PC

24,948

Solution 1

This is not an answer to your direct question, but instead an alternative, and possibly better, way of doing it.

So when you connect in to a computer via Remote Desktop, it removes the Shut Down link from the Start Menu and replaces it with Disconnect or something. However, you can still access the normal shut down functions by pressing Alt + F4 while on the desktop, or after clicking on an empty part of the taskbar.

It should bring up the old fashioned type 'Shut Down Windows' dialog box, with a drop down of all the options you would normally have available in the Start Menu.

This works on 7, Vista and XP.

enter image description here enter image description here

Hope that helps!

Solution 2

I would add the -f switch (force) to your batch file. It forces running applications to close, which is what usually stalls a shutdown.

You can also use the Ctrl-Alt-End combination and restart from the bottom right button:

enter image description here

Solution 3

If it will help, you can save yourself from having to right click and choose Run as administrator using the Elevation PowerToys from Microsoft. They include a tool that lets you run any command as administrator on a console or in a batch file by simply prepending elevate to any command. (Functionality which really should be shipped with Windows.)

To install this functionality, first download the tools, then run the self-extracting executable and extract them to a directory on disk. Navigate to that directory, right click on ElevateCommand.inf, and select Install from the context menu.

Now, edit your batch file, and prepend elevate to your shutdown command, so it looks like this:

elevate shutdown -s -t 5

Now, you can just double-click on your batch script and UAC will automatically prompt you for administrative approval/credentials, no right-clicking required. It is not possible to bypass this dialog without disabling User Account Control.

Share:
24,948

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    I created a batch file to shutdown my PC (Windows 7 Professional).

    The batch file is simple, it contains only 1 line:

    shutdown -s -t 5

    But, sometimes, it does not work.

    I use it by using right-click and then "Run as administrator".

    Is there any better solution for this?

    I need this batch file so I can shutdown my PC while I am connected to it via Remote Desktop. I can turn it on by using wake on lan, but I need a way to completely shut it down remotely!

    Thanks :)

    • Admin
      Admin almost 13 years
      What do you mean by "But, sometimes, it does not work."? Are you refereeing to the fact that you have to run it as administrator or is it simply not working from time to time?
    • Admin
      Admin almost 13 years
      Lol, raise your hand if you tested this on your machine!
    • Admin
      Admin almost 13 years
      No idea what you mean by why it sometimes doesn't work for you, perhaps you have some unsaved documents open with a dialog box showing. Either way check this out also: instructables.com/id/… perhaps that will speed up things for you even more.
    • Eyal
      Eyal almost 13 years
      @David: It certainly can require administrative rights to shut down a machine. It depends on the group policy configuration. Windows Server does not allow non-administrators to shut down. Non-server versions allow it by default, but it can be changed.
    • Eyal
      Eyal almost 13 years
      Why are you looking for a "better" solution than right-clicking on it and running it as administrator? Would you prefer to be told to log out and log back in as an administrator? If you need admin rights to do something, then you need admin rights. There's no workaround unless you own the machine.
    • Eyal
      Eyal almost 13 years
      @Grim: How can that speed things up even more? That's exactly the same thing as he has now. Except, instead of 5 seconds, it waits 10 seconds before initiating a shutdown. That's slower by definition.
  • Eyal
    Eyal almost 13 years
    How in the world would this be a "better" option than what the asker already has?
  • Mason
    Mason almost 13 years
    @Cody Gray Mainly because it is built into Windows, and works every time. It also doesn't limit you to just the Shut Down function. Lastly, I would say it is also quicker to get to. Clicking the taskbar from whatever program you are in and pressing Alt + F4 is quicker than minimizing everything you have open, finding the file, running it, waiting 5 seconds for the timer etc...
  • Eyal
    Eyal almost 13 years
    The shutdown command is built into Windows, too. If you have the necessary privileges to shut down the machine, either method will work. If not, neither method will work. The shutdown command also doesn't limit you to just shutting down (albeit despite the name). There are flags for restarting, etc. The documentation is here.
  • Mason
    Mason almost 13 years
    @Cody Gray Yes, I am aware that the 'Shutdown' command is not limited to just the shutdown functionality. But if you wanted to change what the script did, you would have to manually edit it every time, or create multiple scripts, which just gets messy. How is this preferable to using a drop down window that takes much less time to change?