Blog Archives

Finding wallpaper location in Windows 7 and Windows 8

This article introduces two PowerShell scripts that help you find the wallpaper location in Windows 7 and Windows 8.

View of Garachico, Tenerife, Spain by Diego Delso

View of Garachico, Tenerife, Spain by Diego Delso


Read the rest of this entry

Shutting down, restarting or logging out via shortcuts, batch files and scripts – Part 2

Attachments: Shutdown scripts Shutdown Scripts
This article has multiple attachments

Introducing PsShutdown.exe

In the previous episode, we saw Shutdown.exe, a command-line tool that could perform shutdown, restart or logout. We also saw that the earlier version of Shudown.exe that is included in Windows XP suffers from certain discrepancies that severely limit its usage scenarios.

In addition to Shutdown.exe, a freeware called PsShutdown.exe is also available from Microsoft that features additional capabilities over Shutdown.exe in Windows XP. It can hibernate a computer or lock computer. (Locking a computer returns the user to logon screen without closing his programs. User can return back to work by typing his password and logging back on. An extension of this feature is also known as Switch User.)

However, as of 2 August 2010, the latest version of this tool (2.52.0.0) also suffered from a crippling shortcoming: Only users with administrative privileges could use it. Hence, I will no longer elaborate on it. In fact, if you are using Windows 7 or Windows Server 2008, you are already in possession of the improved version of Shutdown.exe which should be more than enough to handle automatic shutdowns of your local machine. If you are using Windows XP, you still do not need to bicker with Shutdown.exe or PsShutdown.exe: You can create your own shutdown tool!

Creating our own shutdown tool

If you are using Windows XP or Windows Server 2003, you do not have to cope with the discrepancies of Shutdown.exe: You can create your own shutdown tool. If you are using Windows 7 however, you can still create your own shutdown tool, if you like. To do so, you use Windows Script Host (WSH), a Windows component that allows you to run scripts (= simple computer programs) that you yourself have created in JScript and VBScript.

Composing the scripts

Familiarize yourself with the icon of VBScript script files. These files have .VBS file name extension.

You don’t need to create an entire script by yourself: I have already written the scripts for you. Follow the following instructions to import them to your computer.

  1. Start Notepad. You should find it in the Start Menu, under Accessories.
  2. Look below and find the script for the task (shutdown, restart or log out) that you need. (Some tasks may have two scripts written for them. You can choose either of them.)
  3. Copy the entire script into clipboard. To do so, select the script with your mouse. Then, right-click on you selection and from the menu that appears, select Copy.
  4. Paste the entire script into Notepad. To do so, right-click somewhere in the Notepad and from the menu that appears, select Paste.
  5. Save the entire script under a name of your liking but with .vbsextension. If you do not know what I mean, follow these instructions carefully:
    1. In Notepad menu bar, click File, and then select Save As…
    2. Browse to the location in which you’d like to save the file. For example, Desktop.
    3. Click inside File Name field.
    4. Type a single quotation mark (“).
    5. Type a name for your script. Anything will do, but I suggest you type a suitable name.
    6. Type: .vbs (Do not forget the dot.)
    7. Type another quotation mark (“).
    8. Optional: Make sure that Encoding field contains either ANSI or Unicode. It should not contain UTF-8 or Unicode Big Endian.
    9. Press Save button.
  6. Prepare to test the script. To do so, close all your open applications and save any unsaved changes.

Your script should now be ready. If you have named it correctly, it should look like as displayed in the picture. Repeat the steps above to create other scripts that perform other tasks.

Executing the scripts

You can now execute these scripts as you execute normal software applications: Just double-click on them! Make sure you test your scripts.

If you wish, you can place script somewhere outside the immediate eyesight (such a folder in C:\Program Files) and then create a shortcut to those scripts and expose the shortcuts. By doing so, users which have a standard user account may not accidentally modify those files. However, the main advantage of doing so is that you can assign shiny appealing or contextually-meaningful icons to your shortcuts. Yes, appealing icons! Good look and beauty is one of the things that human looks for. You can even find a suitable free third-party icon for the scripts or make one yourself using IcoFX, a free software application. (But I am not going to elaborate on how.)

You might also like to ask other software to run these scripts. For example, I have a 3D rendering application that lets me run an application when the rendering is complete. Sometimes, rendering a piece of 3D work takes a long time, so it might be a good idea to ask this application to use these scripts to shutdown the system after the work is complete. That way, I can leave the system alone.

To execute these scripts from within other software however, you may encounter trouble: Unlike Windows shell, Windows Command Prompt and Windows PowerShell, third-party software may not support direct execution of programs which are not Windows executables, i.e. programs that do not have a .exe file extension. However, there is a workaround for this issue: You must tell such software to run Windows Script Host and pass the path and file name of your script to Windows Script Host. For example, if your shutdown scripts called Shutdown.vbs is saved in C:\Program Files\Admin Scripts, you can tell your third-party software to run this command:

“%systemroot%\system32\wscript.exe” “%ProgramFiles%\Admin Scripts\Shutdown.vbs”

Script codes

Shutdown

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, "Power-off via script"

Const LOGOFF = 0
Const SHUTDOWN = 1
Const REBOOT = 2
Const POWEROFF = 8

Const LOGOFF_FORCE = 4
Const SHUTDOWN_FORCE = 5
Const REBOOT_FORCE = 6
Const POWEROFF_FORCE = 12

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
 ObjOperatingSystem.Win32Shutdown(POWEROFF)
Next

Shutdown (Alternative)

This scripts does exactly what the one above does, only using a different code.

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, "Shutdown via script"

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    ObjOperatingSystem.Shutdown()
Next

Restart

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, "Reboot via script"

Const LOGOFF = 0
Const SHUTDOWN = 1
Const REBOOT = 2
Const POWEROFF = 8

Const LOGOFF_FORCE = 4
Const SHUTDOWN_FORCE = 5
Const REBOOT_FORCE = 6
Const POWEROFF_FORCE = 12

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
 ObjOperatingSystem.Win32Shutdown(REBOOT)
Next

Restart (Alternative)

Like the script above, the following script also restarts the computer:

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, "Shutdown via script"

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    ObjOperatingSystem.Reboot()
Next

Log off

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, "Log-off via script"

Const LOGOFF = 0
Const SHUTDOWN = 1
Const REBOOT = 2
Const POWEROFF = 8

Const LOGOFF_FORCE = 4
Const SHUTDOWN_FORCE = 5
Const REBOOT_FORCE = 6
Const POWEROFF_FORCE = 12

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
 ObjOperatingSystem.Win32Shutdown(LOGOFF)
Next

Further reading

For more information about these scripts, see:

  1. “Shutting Down Computers and Logging Off Users”. Microsoft TechNet Scripting Center. Microsoft corporation.
  2. “Shutting Down a Computer”. Microsoft TechNet Scripting Center. Microsoft corporation.
  3. “Restarting a Computer”. Microsoft TechNet Scripting Center. Microsoft corporation.
  4. “Win32Shutdown Method of the Win32_OperatingSystem Class”. Microsoft Developers Network (MSDN) Library. Microsoft corporation. 9 September 2010.
  5. “Reboot Method of the Win32_OperatingSystem Class”. Microsoft Developers Network (MSDN) Library. Microsoft corporation. 9 September 2010.
  6. “Shutdown Method of the Win32_OperatingSystemClass”. Microsoft Developers Network (MSDN) Library. Microsoft corporation. 9 September 2010.

What’s next?

I promised to blog about Windows PowerShell cmdlets that allows system administrators to perform shutdown and restart. I will leave that to another blog post.

Shutting down, restarting or logging out via shortcuts, batch files and scripts – Part 1

What I am going to write is already published multiple times on the Internet, but I’d like to keep the knowledge that I use on my own blog. After all, Top Secret Files! is to serve as a reference for all, including me.

All of us know how to use Start Menu to shut down, restart or hibernate or computers or log out of our user accounts. But not all of us know how to do so via a shortcut, from Command Prompt, from Windows PowerShell, from within a batch file or from within a script. In this blog post, I’d like to introduce you to Shutdown.exe and its ability to perform said actions via a shortcut, from Command Prompt or from within a batch file.

Shutdown.exe

Shutdown notice on Windows 7 Shutdown.exe is a dedicated program for performing shutdown, restart and logout that comes with Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008 and Windows 7. (It is also available on Windows 2000 Resource Kit disc.) It can be used to shut down or restart remote computers as well. It can also make Windows to shut down or restart either immediately or after a countdown timer expires.

Unfortunately shutdown.exe cannot hibernate computers on Windows XP or earlier. Moreover, Shutdown.exe cannot put the computer into sleep mode (also known as standby mode). Most importantly however, Shutdown.exe in Windows XP requires administrative privileges to shutdown or restart local machine! By default, guests and standard users who can normally shut down or restart local computer via Start Menu or other means cannot do so via Shutdown.exe. If they try to do so, they will encounter the following cryptic error and misleading message:

Operation completed successfully.
A required privilege is not held by the client.

Shutdown notice on Windows XP

The default behavior of this command in Windows XP can be modified by editing a local security policy item called “Force shutdown from a remote system” (and suffering the consequences of doing so).

Fortunately in Windows 7, Shutdown.exe can, by default, shutdown or restart without requiring administrative privileges or unusual user rights in the security policy.

Creating shortcuts that perform shutdown

You can create shortcuts to shutdown.exe on your desktop (or anywhere else) that shutdown, restart or hibernate your computer or log you out of the current user account.

To so:

  1. Go to your desktop or any folder in which you’d like to create a shortcut. You can press Windows key+D on your keyboard to reveal your desktop immediately.
  2. Bring up the context-menu. To do so, right-click on an empty space on your desktop or the folder in which you’d like to place the shortcut. Alternatively, press CTRL+Space to deselect any selected item and then press Context-Menu key on your keyboard. A menu should appear.
  3. From the menu, select “New” and then select “Shortcut”.
  4. Enter one of the commands specified in the Supported Actions table below that corresponds to the purpose of your shortcut. For example, if you are creating a shortcut to restart your Windows XP computer, enter: shutdown.exe -r -t 0.
  5. Press Next key.
  6. Enter a name for the shortcut. In case of example above, enter: Restart computer.
  7. Press Finish key.

You can create more complex shortcut by customizing the command that you entered in step 4. To do so, study the syntax of shutdown.exe command further below. You can create shortcuts that shut down local machine after a delay, shut down a remote machine (provided that you have enough privileges on it), etc.

Performing shutdown from Command Prompt and batch files

To shut down, restart or hibernate your computer or log out of your user account from Command Prompt, just enter a valid shutdown.exe command (like the one included in table below) and press ENTER. It will behave just like the shortcut. Same command can be entered in a batch file to produce the same effect.

Performing shutdown from Windows PowerShell and scripts

It is possible to enter the same shutdown.exe command that you’d enter in Command Prompt in Windows PowerShell command line or scripts and get the same result. However, Windows PowerShell has dedicated “cmdlets” (internal commands) that can do such a job. I will write about these command in my next blog post.

Also in case of Windows Scripting Host, you can write VBScripts or JScripts that run shutdown.exe and pass necessary parameters to it. However, I will also not explore this avenue because in my next blog post I’ll be teaching you how to use scripts to shutdown Windows without using shutdown.exe.

Supported actions

The table below lists the supported actions by shutdown.exe:

Action Command in Windows XP Command in Windows 7
Immediately shutdown local computer shutdown.exe -s -t 0 shutdown /s /t 0
— or —
shutdown /p
Immediately restart local computer shutdown.exe -r -t 0 shutdown /r /t 0
Immediately put the local computer to sleep (Not supported) (Not supported)
Immediately hibernate the local computer (Not supported) shutdown.exe /h
Immediately log out of the current user account shutdown.exe -l 0 shutdown.exe /l

Syntax

I have included the syntax of both Windows XP’s shutdown.exe and Windows 7’s shutdown.exe for you to compare.

Here is the shutdown.exe syntax in Windows XP (modified a bit to fit this blog post):

Usage: shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computer] [-t xx] [-c "comment"] [-d up:xx:yy]

   No args          Display this message (same as -?)
   -i               Display GUI interface, must be the first option
   -l               Log off (cannot be used with -m option)
   -s               Shutdown the computer
   -r               Shutdown and restart the computer
   -a               Abort a system shutdown
   -m \\computer    Remote computer to shutdown/restart/abort
   -t xx            Set timeout for shutdown to xx seconds
   -c "comment"     Shutdown comment (maximum of 127 characters)
   -f               Forces running applications to close without warning
   -d [u][p]:xx:yy  The reason code for the shutdown
                    u is the user code
                    p is a planned shutdown code
                    xx is the major reason code (positive integer less than 256)
                    yy is the minor reason code (positive integer less than 65536)

Here is the shutdown.exe syntax in Windows 7 (modified a bit to fit this blog post):

Usage: shutdown [/i | /l | /s | /r | /g | /a | /p | /h | /e] [/f]
    [/m \\computer][/t xxx][/d [p|u:]xx:yy [/c "comment"]]

    No args    Display help. This is the same as typing /?.
    /?         Display help. This is the same as not typing any options.
    /i         Display the graphical user interface (GUI).
               This must be the first option.
    /l         Log off. This cannot be used with /m or /d options.
    /s         Shutdown the computer.
    /r         Shutdown and restart the computer.
    /g         Shutdown and restart the computer. After the system is
               rebooted, restart any registered applications.
    /a         Abort a system shutdown.
               This can only be used during the time-out period.
    /p         Turn off the local computer with no time-out or warning.
               Can be used with /d and /f options.
    /h         Hibernate the local computer.
               Can be used with the /f option.
    /e         Document the reason for an unexpected shutdown of a computer.
    /m \\computer Specify the target computer.
    /t xxx     Set the time-out period before shutdown to xxx seconds.
               The valid range is 0-315360000 (10 years), with a default of 30.
               If the timeout period is greater than 0, the /f parameter is
               implied.
    /c "comment" Comment on the reason for the restart or shutdown.
               Maximum of 512 characters allowed.
    /f         Force running applications to close without forewarning users.
               The /f parameter is implied when a value greater than 0 is
               specified for the /t parameter.
    /d [p|u:]xx:yy  Provide the reason for the restart or shutdown.
               p indicates that the restart or shutdown is planned.
               u indicates that the reason is user defined.
               If neither p nor u is specified the restart or shutdown is
               unplanned.
               xx is the major reason number (positive integer less than 256).
               yy is the minor reason number (positive integer less than 65536).

Reasons on this computer:
(E = Expected U = Unexpected P = planned, C = customer defined)
Type    Major   Minor   Title

 U      0       0       Other (Unplanned)
E       0       0       Other (Unplanned)
E P     0       0       Other (Planned)
 U      0       5       Other Failure: System Unresponsive
E       1       1       Hardware: Maintenance (Unplanned)
E P     1       1       Hardware: Maintenance (Planned)
E       1       2       Hardware: Installation (Unplanned)
E P     1       2       Hardware: Installation (Planned)
E       2       2       Operating System: Recovery (Planned)
E P     2       2       Operating System: Recovery (Planned)
  P     2       3       Operating System: Upgrade (Planned)
E       2       4       Operating System: Reconfiguration (Unplanned)
E P     2       4       Operating System: Reconfiguration (Planned)
  P     2       16      Operating System: Service pack (Planned)
        2       17      Operating System: Hot fix (Unplanned)
  P     2       17      Operating System: Hot fix (Planned)
        2       18      Operating System: Security fix (Unplanned)
  P     2       18      Operating System: Security fix (Planned)
E       4       1       Application: Maintenance (Unplanned)
E P     4       1       Application: Maintenance (Planned)
E P     4       2       Application: Installation (Planned)
E       4       5       Application: Unresponsive
E       4       6       Application: Unstable
 U      5       15      System Failure: Stop error
 U      5       19      Security issue
E       5       19      Security issue
E P     5       19      Security issue
E       5       20      Loss of network connectivity (Unplanned)
 U      6       11      Power Failure: Cord Unplugged
 U      6       12      Power Failure: Environment
  P     7       0       Legacy API shutdown

Stay tuned

In the next blog post, I’ll about other avenues of automated shutdown via Windows Scripts and Windows PowerShell.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: