Using Windows Task Scheduler to put PCs into standby or hibernate mode

Scheduled tasks can be used to put computers into standby or hibernate mode using a combination of VBScript and PsShutdown from Microsoft (formerly from Sysinternals) — provided users are logged off. PsShutdown can be downloaded from Microsoft’s website.

This method has an advantage over using Power Policies to control power management: you can force logged-out PCs to go into standby mode, while allowing users to control their own Power Policies when logged on.  However, forcing PCs into standby mode should only be necessary only if the ability for applications to veto sleep mode has been enabled through group policy.  If this setting has been enabled you may want to talk to you network administrator about the group policy settings.

Scheduling the Task

In the following example a task is set to run every hour. This scheduled task will put logged-off computers into standby mode, without disturbing computers that have users logged on. If users don’t have rights to schedule tasks, the script can be run as a computer logon script in Group Policy. This schedule is only one example. Tasks could be scheduled to run every half hour between 5 pm and 7 am, for example. To see all options for scheduling tasks type "schtasks.exe /?" without the quotes in a command prompt on the computer that will be scheduling the tasks (the client computer if the tasks are being scheduled through a logon script.)

SCHTASKS.exe /Create /S %computername% /RU "SYSTEM" /ST 00:00:00 /SC HOURLY /SD 01/01/2007 /TN "StandBy" /TR "cscript.exe C:\ValidPath\standby-hibernate.vbs

The Script

This script assumes that PsShutdown.exe exists in the %windows% directory. The directory can be changed to any valid path where PsShutdown.exe exists and is accessible by the computer’s SYSTEM account.

'** Script Name: "standby-hibernate.vbs" **
Option Explicit
On Error Resume Next
Dim strComputer, sUserName, bLoggedOn, bReboot, objWMIService, 
	colComputer, objComputer
Dim bStandby, objShell
strComputer = "."

Set objShell = WScript.CreateObject("Wscript.Shell")
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer 
	& "\root\cimv2") 
Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
	sUserName = objComputer.UserName
	'WScript.Echo "UserName:  " & objComputer.UserName
 	If sUserName  <> "null" Then
 		bLoggedOn = True
 	End If

If Err = 0 Then
	If bLoggedOn Then
		WScript.Echo strComputer & " 
			is not Logged Off."
		bStandby = False
		WScript.Echo strComputer & " 
			is Logged Off."
		bStandby = True
	End If
	WScript.Echo "Error accessing computer:  " 
		& strComputer
	bStandby = False
End If	
On Error Goto 0

WScript.Echo "bStandby:  " & bStandby
If bStandby = True Then
	WScript.Echo "Going into standby..."
	'Go to standby "%windir%\psshutdown.exe -d -accepteula", 
		0, False
	WScript.Echo "Not going into standby..."
End If