PowerShell hibernation script RRS feed

  • Question

  • Hi folks!

    I'm trying to setup a hibernation method on my organization (we intent to apply it to 500 client pcs) and looking around I came across this PS Script to implement it as a scheduled task on one the servers.

    Add-Type -AssemblyName System.Windows.Forms
    function Set-PowerState {
        param (
              [System.Windows.Forms.PowerState] $PowerState = [System.Windows.Forms.PowerState]::Hibernate
            , [switch] $DisableWake
            , [switch] $Force
        begin {
            Write-Verbose -Message 'Executing Begin block';
            if (!$DisableWake) { $DisableWake = $false; };
            if (!$Force) { $Force = $True; };
            Write-Verbose -Message ('Force is: {0}' -f $Force);
            Write-Verbose -Message ('DisableWake is: {0}' -f $DisableWake);
        process {
            Write-Verbose -Message 'Executing Process block';
            try {
                $Result = [System.Windows.Forms.Application]::SetSuspendState($PowerState, $Force, $DisableWake);
            catch {
                Write-Error -Exception $_;
        end {
            Write-Verbose -Message 'Executing End block';
    # Call the function
    Set-PowerState -PowerState Hibernate -DisableWake -Force;

    If I use the Invoke-Command manually it works as a intended although PowerShell keeps running the script and shows me

    WARNING: The network connection to PC343 has been interrupted. Attempting to reconnect for up to 4 minutes...
    WARNING: Attempting to reconnect to PC343 ...

    Until I wake them up, where it shows

    WARNING: The network connection to PC343 has been restored

    And gives me back the prompt.

    On the other hand, when I set it up as a task using task scheduler it does not work. I have a scheduled task set up as follows:

    • Open a program: powershell.exe
    • Arguments: -file "C:\HibernateTest.ps1" -ExecutionPolicy Bypass

    HibernateTest.ps1 has the following code:

    invoke-command -computername (Get-Content "C:\HibernateTest.csv") -filepath "C:\Hibernate.ps1"

    On task manager I see "Task is currently running", but PowerShell is not present on task manager and the computers do not hibernate.

    I would really like some guidance on this, since there's obviously something I'm not seeing... Thank you in advance for your time!
    • Moved by Bill_Stewart Friday, July 27, 2018 8:40 PM This is not "fix/debug/rewrite this script I found on the Internet" forum
    Monday, May 28, 2018 1:48 PM

All replies

  • We cannot help you with scripts found on the Internet and this is not a task scheduler forum.


    Monday, May 28, 2018 1:56 PM