Results 1 to 1 of 1

Thread: Laptop Shutdown/Standby/Hibernate Software Script

  1. #1
    Newbie
    Join Date
    Dec 2007
    Posts
    9

    Laptop Shutdown/Standby/Hibernate Software Script

    This is a script I developed to automatically put a laptop running Microsoft Windows 7 into Standby or Hibernate.
    I developed this because ShutControl wasn't reliable for me - it would continually corrupt the config file and crash with .NET errors.

    The software is provided as-is, and you are free to do whatever with it. No warranties if you break your stuff with it. It works for me and is simple and gets the job done.

    Requirements:
    Laptop (with a working battery which holds charge for at least 1 minute)
    Windows 7 (may work on Vista as well but untested)
    Windows Powershell (built into Windows 7)

    Preparation:
    1. Start Powershell from the start menu as an Administrative User
    2. Type the following:
    Code:
    Set-ExecutionPolicy RemoteSigned
    Yes
    Exit
    3. Close Powershell
    4. Start Powershell IME as a regular user
    5. Paste the following code into it and save it somewhere such as your Documents folder with the file name Test-IsOnBattery.ps1
    Code:
    # Test-IsOnBattery.ps1 version 1.0 by dilmah @ mp3car.com 2011-05-17
    # ============================================================================
    # DebugMode:   Set to True to get extra info printed to console
    #              Set to False for production environment
    # Computer:    Run script on localhost
    # StandbyMode: Set to 0 for Standby, 1 for Hibernate
    # CheckFreq:   How often (in seconds) to check battery status
    # ResumeDelay: How long to wait (in seconds) after resume before checking again
    Param($DebugMode = $True, $Computer = "localhost", $StandbyMode = 0, `
      $CheckFreq = 5, $ResumeDelay = 30)
    
    # ============================================================================
    # Function to help print status messages to screen
    # ============================================================================
    Function PrintStatus([string]$Status)
    {
      if ($DebugMode -eq $True)
      {
        $Now = Get-Date -UFormat "%d/%m/%Y %T"
        write-host "$Now : $Status"
      }
    }
    
    # ============================================================================
    # Function to test if computer is on battery
    # ============================================================================
    Function Test-IsOnBattery
    {
      Param([string]$Computer)
      # Get battery status
      [BOOL](Get-WmiObject -Class BatteryStatus -Namespace root\wmi `
        -ComputerName $Computer).PowerOnLine
    }
    
    # ============================================================================
    # Function to put computer into standby mode
    # ============================================================================
    Function GoToStandby
    {
      [Reflection.Assembly]::LoadwithPartialname("System.Windows.Forms") > $null
      $Return = [System.Windows.Forms.Application]::SetSuspendState($StandbyMode,0,0);
    }
    
    # ============================================================================
    # Program Start
    # ============================================================================
    PrintStatus("Monitoring commenced")
    # Infinite loop to check battery power status
    for (;;)
    {
      $result = Test-IsOnBattery -computer $computer
      # If computer is powered by battery only, prepare to enter power saving mode
      if ($result -eq $False) {
        PrintStatus("Computer on battery (recheck in $CheckFreq seconds)")
        # Wait & check again if still on battery
        Start-Sleep -s $CheckFreq
        $result = Test-IsOnBattery -computer $computer
        if ($result -eq $False) {
        # Still on battery, initiate transition to standby mode
          PrintStatus("Computer on battery still")
          PrintStatus("Computer standby")
          GoToStandby
          PrintStatus("Computer resumed")
          # After resume, wait a few seconds - just in case AC power is unstable
          # i.e. during engine cranking when connected to car cigarette lighter etc
          Start-Sleep -s $ResumeDelay
          PrintStatus("Monitoring resumed")
        } else {
          # Power restored, no need to enter power saving mode
          PrintStatus("Computer not on battery")
        }
      }
      # Wait $CheckFreq seconds before checking again
      # Don't set this variable too low, may consume too many system resources
      Start-Sleep -s $CheckFreq
    }
    6. Test it by clicking on the green Run button. Unplug power for 5 seconds, replug for 5 seconds, unplug for 10 seconds, reconnect power and watch the status messages if you have any problems. The script is well commented to help you troubleshoot.
    7. For production use (once you're happy with it), change $DebugMode = $True to $DebugMode = $False
    8. Make a shortcut in your Startup folder of your start menu as below and set it to start in a Minimised window:
    Code:
    powershell.exe <path to Test-IsOnBattery.ps1>
    Any queries please feel free to ask and I'll do my best to answer them.
    This is the first time I've ever scripted something in Powershell, so please be nice.

    Dilmah.
    Last edited by dilmah; 05-17-2011 at 01:16 AM.

Similar Threads

  1. laptop standby -> hibernate on battery doesn't work
    By dazman2 in forum How to Power a Laptop
    Replies: 8
    Last Post: 10-06-2008, 08:05 AM
  2. Replies: 2
    Last Post: 06-17-2007, 01:03 PM
  3. Laptop Statup/Shutdown/standby/hibernate question
    By phyter23 in forum Laptops, Netbooks, Tablets, UMPCs, etc.
    Replies: 29
    Last Post: 01-28-2006, 02:01 AM
  4. ShutDown, Hibernate, Standby Code
    By alti in forum Software & Software Development
    Replies: 5
    Last Post: 08-10-2005, 05:12 PM
  5. Standby, Shutdown or hibernate???
    By futuresweets10 in forum General Hardware Discussion
    Replies: 11
    Last Post: 10-19-2004, 08:26 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •