Thursday, August 4, 2011

Windows - How to shutdown computer using command line or batch file?

There are times that we want to insert codes in our program to automatically do things in a different instance other than your running program. For example, we want to insert batch file in our program to automatically shutdown a workstation. To do this here is the simple code.

  1. Run a simple editor like notepad.
  2. To create our batch file, write down the following code below:
    @echo off
    %windir%\system32\shutdown.exe -s -f -t 20

    LEGEND:
    -s = shutdown
    -f = force close any running application
    -t = timer
    20 = no. of seconds left before the system shutdown

  3. Save your work as a ".bat" file.
  4. Done.
You can now call on a batch file to shutdown your computer. However, in Windows 2000, there is a slight difference in making this happen. See the instructions below.

  1. Run a simple editor like notepad.
  2. To create our batch file, write down the following code below:

    @echo off
    shutdown /l /t:2 /y /c

    LEGEND:
    /l (Note that this is a lowercase "L" character) = Use this switch to shut down the local computer
    /t:xx = Use this switch to specify the time (in seconds) after which the computer is shut down. The default is 20 seconds.
    /y = Use this switch to force a "yes" answer to all queries from the computer.
    /c = Use this switch quit all running programs. If you use this switch, Windows forces all programs that are running to quit. The option to save any data that may have changed is ignored. This can result in data loss in any programs for which data is not previously saved.

  3. Save your work as a ".bat" file.
  4. Done.

NOTE: This will only work on Windows 2000 with System Resource Kit that comes with its installation CD. BUT, I have managed to find the exact tool in the Windows 2000 Resource Kit to let us shutdown our Windows 2000 running computer. Download it here. After downloading the file, extract it and put the shutdown.exe in "C:\WINNT\system32\" folder. Your batch file should be working after that.

Enjoy the snippet and have fun.

Continue coding.

No comments:

Post a Comment