vbScript: Tweaking Power Settings (disabling hibernate and standby)

As is often the case in IT, when you need to push out that software package or migrate that computer to a new domain, it isn’t on the network.  This has come up several times in the past year and I wanted to share my solution.  Now, this isn’t the “greenest” solution because this will ensure your clients never go into a power saving mode.  However, it can be a temporary fix for a project.  It can also be adapted to force standby or hibernate at specific thresholds.

While Windows 7 and Vista make this task simple, XP is still a reality in most enterprises and SMB’s and therefore, must be taken into account.  The script below does just that.  Some additional examples can be found at the US Government’s Energy Star website.

Note: Copying the script from the webpage may cause formatting issues.  You can download 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
		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
	' 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
		' 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
			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 


  1. Nice script, however; it should check for the installed loction 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 objFSO : Set objFSO = CreateObject(“Scripting.FileSystemObject”)
    If objFSO.FileExists(filePath) = False Then wscript.quit

    1. I suppose. It would be more portable w/ that add. The environment I managed w/ the script (and every environment I have seen) has the system root as c:windows. So, I didn’t see the need to add the extra step. Good add though. That should work for non-standard installations.

  2. Hey Andrew, thanks for the post. You mention Windows 7 “makes this simple” – can you elaborate on that? What would be the scripting approach to change these settings in Win7?

Comments are closed.