Using powercfg with vbscript

Posted on 2007-12-06
Last Modified: 2012-08-14
Hi Experts
i have a piece of code here need some help, i couldnt get it run smoothly. my objective is to set the power management option with vbs. is there anything wrong with my sytax?

Dim objShell:Set objShell=CreateObject("Wscript.Shell")
On Error Resume Next
With objShell
  .Run "cmd powercfg /setactive "Home/Office Desk""
  .Run "cmd powercfg /x "Home/Office Desk" /monitor-timeout-ac 0"
  .Run "cmd powercfg /x "Home/Office Desk" /monitor-timeout-dc 0"
  .Run "cmd powercfg /x "Home/Office Desk" /disk-timeout-ac 0"
  .Run "cmd powercfg /x "Home/Office Desk" /disk-timeout-dc 0"
  .Run "cmd powercfg /x "Home/Office Desk" /standby-timeout-ac 0"
  .Run "cmd powercfg /x "Home/Office Desk" /standby-timeout-dc 0"
  .Run "cmd powercfg /x "Home/Office Desk" /hibernate-timeout-ac 0"
  .Run "cmd powercfg /x "Home/Office Desk" /hibernate-timeout-dc 0"
End With

Open in new window

Question by:Manfredtoo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 30

Assisted Solution

SteveGTR earned 50 total points
ID: 20421087
Use the /c switch:

.Run "cmd /c powercfg /setactive "Home/Office Desk"


Author Comment

ID: 20421284
thanks steve got it =)

Author Comment

ID: 20421411
steve: i tried again, it shows me error: expected end of statement.
Containers and Docker for Everyone

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

LVL 12

Accepted Solution

chandru_sol earned 450 total points
ID: 20421466
Hope this will help

Set oShell = CreateObject("Wscript.Shell")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
OS = LEFT(objOperatingSystem.Version,3)
If( UCase( Trim( OS ) ) = UCase( Trim( 5.1 ) ) ) Then
oShell.Run "powercfg.exe -change ""Home/Office Desk"" -monitor-timeout-ac 15", 0, True
oShell.Run "powercfg.exe -change ""Home/Office Desk"" -disk-timeout-ac 25", 0, True
oShell.Run "powercfg.exe -change ""Home/Office Desk"" -standby-timeout-ac 0", 0, True
oShell.Run "powercfg.exe -change ""Home/Office Desk"" -hibernate-timeout-ac 0", 0, True
oShell.Run "powercfg.exe -hibernate off", 0, True
oShell.Run "powercfg.exe -setactive ""Home/Office Desk""", 0, True
End If

Open in new window


Author Comment

ID: 20421517
chandru: it works great =) thanks alot. hey been seeing u helping me quite a few times. really appreciate it
LVL 12

Expert Comment

ID: 20421538
Glad i was of help! No worries


Author Closing Comment

ID: 31413189
powercfg with vbs

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question