vbScript: Tweaking Power Settings (disabling hibernate and standby)

As is often the case in IT, when you need to push out that soft­ware pack­age or migrate that com­put­er to a new domain, it isn’t on the net­work.  This has come up sev­er­al times in the past year and I want­ed to share my solu­tion.  Now, this isn’t the “green­est” solu­tion because this will ensure your clients nev­er go into a pow­er sav­ing mode.  How­ev­er, it can be a tem­po­rary fix for a project.  It can also be adapt­ed to force stand­by or hiber­nate at spe­cif­ic thresh­olds.

While Win­dows 7 and Vista make this task sim­ple, XP is still a real­i­ty in most enter­pris­es and SMB’s and there­fore, must be tak­en into account.  The script below does just that.  Some addi­tion­al exam­ples can be found at the US Gov­ern­men­t’s Ener­gy Star web­site.

Note: Copy­ing the script from the web­page may cause for­mat­ting issues.  You can down­load the script here: tweak-power-settings.vbs

'======================================================
' VBScript Source File
' NAME: Tweak Power Settings
' AUTHOR: Andrew Healey (andrew -at- healey -dot- io)
' WEB: https://www.healey.io
' DATE  : 2010.07.15 (updated 2011.05.03)
' COMMENT: This script will disable hibernate and set strict power settings
'			to ensure the system does not power off, hibernate or go into 
'			standby. This will work on all client operating systems 2000 
'			and newer.
'======================================================


On Error Resume Next

' Basic checks to see if this will even work and exit if not
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("c:windowssystem32powercfg.exe") = False Then wscript.quit
If Err Then wscript.quit

' Call client os function to determine if it is a server or not
' This script needs modification to run against servers as it is 
' meant only for client operating systems.
Dim strOS : strOS = isClientOperatingSystem()
If strOS = False Then wscript.quit

Dim objWshShell : Set objWshShell = WScript.CreateObject("WScript.Shell")

' Disable hibernate if it is enabled
If objFSO.FileExists("c:hiberfil.sys") Then
	If InStr(strOS,"XP") > 0 Then
		objWshShell.Run "powercfg /hibernate off", 0, True
	Else
		objWshShell.Run "powercfg -h off", 0, True
	End If
End If

' Turn off standby and monitor timeout (while plugged in)
If InStr(strOS,"XP") > 0 Or InStr(strOS,"2000") > 0 Then
	' XP and 2000 specific settings
	objWshShell.Run "powercfg /X " & chr(34) & "home/office desk" & chr(34) & _
					" /standby-timeout-ac 0",0,True
	objWshShell.Run "powercfg /X " & chr(34) & "home/office desk" & chr(34) & _
					" /monitor-timeout-ac 0",0,True
	objWshShell.Run "powercfg /setactive " & chr(34) & "home/office desk" & chr(34),0,True
Else
	' Vista, 7
	objWshShell.Run "powercfg -s 381B4222-F694-41F0-9685-FF5BB260DF2E",0,True
	objWshShell.Run "powercfg -change -standby-timeout-ac 0",0,True
	objWshShell.Run "powercfg -change -monitor-timeout-ac 0",0,True
End If

' Clean house
Set objWshShell = Nothing
Set objFSO = Nothing


Private Function isClientOperatingSystem()
	' Purpose: This will return true only if the system can be verified as a client OS
	' Usage: strOS = isClientOperatingSystem()
	'		 If strOS = False Then wscript.quit
	Dim objWMIService, objItem, colItems
	Dim strOS

	On Error Resume Next
		' WMI Connection to the object in the CIM namespace
		Set objWMIService = GetObject("winmgmts:\.rootcimv2")

		' WMI Query to the Win32_OperatingSystem
		Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

		' For Each... In Loop (Next at the very end)
		For Each objItem in colItems
			strOS = objItem.Caption
		Next
		
		' Below might be a better way but I want to make sure it is one of the operating systems that has been tested, YMMV
		' InStr(strOS,"Server") > 0 Then isClientOperatingSystem = False
		If InStr(strOS,"Windows 7") > 0 Or InStr(strOS,"XP") > 0 Or InStr(strOS,"2000 Professional") > 0 Or InStr(strOS,"Vista") > 0 Then
			isClientOperatingSystem = strOS
		Else
			isClientOperatingSystem = False
		End If
		
		If Err.Number > 0 Then isClientOperatingSystem = False
		
		strOS = Empty
		Set objItem = Nothing
		Set colItems = Nothing
		Set objWMIService = Nothing
	On Error GoTo 0
End Function 

4 comments

  1. Nice script, how­ev­er; it should check for the installed loc­tion of the OS to see if powergfg.exe exists.

    ’ Basic checks to see if this will even work and exit if not

    Set WSHShell = WScript.CreateObject ( “WScript.Shell” )
    fldr = WSHShell.ExpandEnvironmentStrings(“%SystemRoot%”)
    filePath = fldr & “system32powercfg.exe”

    Dim objF­SO : Set objF­SO = CreateObject(“Scripting.FileSystemObject”)
    If objFSO.FileExists(filePath) = False Then wscript.quit

    1. I sup­pose. It would be more portable w/ that add. The envi­ron­ment I man­aged w/ the script (and every envi­ron­ment I have seen) has the sys­tem root as c:windows. So, I did­n’t see the need to add the extra step. Good add though. That should work for non-stan­dard instal­la­tions.

  2. Hey Andrew, thanks for the post. You men­tion Win­dows 7 “makes this sim­ple” - can you elab­o­rate on that? What would be the script­ing approach to change these set­tings in Win7?

Comments are closed.