Suspend or hibernate from PowerShell

前端 未结 4 1999
一生所求
一生所求 2021-02-01 20:49

I am interested in using Windows PowerShell to suspend or hibernate a computer. How do you achieve this?

I am already aware of the Stop-Computer and R

相关标签:
4条回答
  • 2021-02-01 20:49

    Hope you find these useful.

    Shutdown %windir%\System32\shutdown.exe -s

    Reboot %windir%\System32\shutdown.exe -r

    Logoff %windir%\System32\shutdown.exe -l

    Standby %windir%\System32\rundll32.exe powrprof.dll,SetSuspendState Standby

    Hibernate %windir%\System32\rundll32.exe powrprof.dll,SetSuspendState Hibernate

    EDIT: As pointed out in comment by @mica, the suspend (sleep) actually hibernates. Apparently this happens in windows 8 and above. To 'sleep', disable hibernation OR get an external Microsoft tool (not built-in) "One of Microsoft's Sysinternals tool is PsShutdown using the command psshutdown -d -t 0 it will correctly sleep, not hibernate, a computer" Source: https://superuser.com/questions/42124/how-can-i-put-the-computer-to-sleep-from-command-prompt-run-menu

    0 讨论(0)
  • 2021-02-01 20:49

    I use the shutdown executable in C:\Windows\System32

    shutdown.exe /h
    
    0 讨论(0)
  • 2021-02-01 21:08

    You can use the SetSuspendState method on the System.Windows.Forms.Application class to achieve this. The SetSuspendState method is a static method.

    [MSDN] SetSuspendState

    There are three parameters:

    • State [System.Windows.Forms.PowerState]
    • Force [bool]
    • disableWakeEvent [bool]

    To call the SetSuspendState method:

    # 1. Define the power state you wish to set, from the
    #    System.Windows.Forms.PowerState enumeration.
    $PowerState = [System.Windows.Forms.PowerState]::Suspend;
    
    # 2. Choose whether or not to force the power state
    $Force = $false;
    
    # 3. Choose whether or not to disable wake capabilities
    $DisableWake = $false;
    
    # Set the power state
    [System.Windows.Forms.Application]::SetSuspendState($PowerState, $Force, $DisableWake);
    

    Putting this into a more complete function might look something like this:

    function Set-PowerState {
        [CmdletBinding()]
        param (
              [System.Windows.Forms.PowerState] $PowerState = [System.Windows.Forms.PowerState]::Suspend
            , [switch] $DisableWake
            , [switch] $Force
        )
    
        begin {
            Write-Verbose -Message 'Executing Begin block';
    
            if (!$DisableWake) { $DisableWake = $false; };
            if (!$Force) { $Force = $false; };
    
            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;
    

    Note: In my testing, the -DisableWake option did not make any distinguishable difference that I am aware of. I was still capable of using the keyboard and mouse to wake the computer, even when this parameter was set to $true.

    0 讨论(0)
  • 2021-02-01 21:09

    I tried reducing this down to a one-liner but was getting an error. Here's my solution:

    [Void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    [System.Windows.Forms.Application]::SetSuspendState("Hibernate", $false, $false);
    
    0 讨论(0)
提交回复
热议问题