Scheduling Sleep Mode via the Task Scheduler

Information
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. Forcing PCs into standby mode may be necessary in Windows 2000 and XP, because these operating systems allow some software applications to "veto" standby and hibernate. As a result, you may find your computers failing to go to sleep — even when logged off. The same problem may appear on Windows Vista PCs, but 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. More information about using group policy in Vista is available on Microsoft’s TechNet




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

Code:
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.

Code:
'** 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
Next

If Err = 0 Then
If bLoggedOn Then
WScript.Echo strComputer & "
is not Logged Off."
bStandby = False
Else
WScript.Echo strComputer & "
is Logged Off."
bStandby = True
End If
Else
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
objShell.run "%windir%\psshutdown.exe -d -accepteula",
0, False
Else
WScript.Echo "Not going into standby..."
End If
Resources
Tutorial Source: Energy Star
Microsoft Program: PsShutdown.exe