Skip to main content

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.

'======================================================
' 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 

Related

.Net Classes within VBScript: Doing Randomness and Arrays the Easy Way

·2 mins
Back in 2007, the Microsoft Scripting Guys posted a article titled “Hey, Scripting Guy! Be Careful What You Say.” This article changed everything in the way I scripted because it should how simply you can access some .Net classes through COM callable wrappers. The two they focus on are “System.Random” and “System.Collections.ArrayList”. Set objRandom = CreateObject("System.Random") Set objArrList = CreateObject("System.Collections.ArrayList") ArrayList # When scripting in AD, Exchange or even the desktop, I am consistently working with arrays. Adding items and sorting arrays always required custom functions, overly wordy statements or re-dimensioning. This made working with arrays cumbersome. Their example for sorting the non-.net way:

vbScript: Quickly determine architecture

I’ve been using a routine to determine 64-bit v 32-bit workstations for some time checking the registry for the PROCESSOR\_ARCHITECTURE in the HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment path. However, this was proving to be error prone. So, I just gave up that method altogether since all Windows x64 editions have a “%SystemDrive%Program Files (x86)” directory. This makes it just a quick and easy call the folderexists method of the filesystemobject. The only downside is that can’t be used remotely but since most of my scripts are used in local policies, this shouldn’t be an issue.

Installing Java via Script and Group Policy

Due to some software requirements, there was a need to get JRE 1.5.0_09 rolled out across our enterprise. The requirements were pretty straight forward: Only install on client operating systems (Windows 2000, Windows XP, Windows Vista and Windows 7) Detect the versions of Java installed. If 1.5.0_09 is installed, exit. If 1.5.0_08 or less was installed, install this version. If it has a newer version, do nothing. The best way of determining the Java versions is to look in %program files%. On 64-bit machines, this is “C:program files (x86)Java”. On 32-bit, this is “C:program filesJava”. The script accounts for this.