How to Powershell Script to Sleep Remote Computer | How to Guide - Action1

Author: Peter Barnett           Date: Apr 12, 2019


There are quite a few methods for working with remote computers. There is Windows Management Instrumentation (WMI) widely used in VBScript. There are various utilities that allow remote control, such as PSExec from Sysinternals. Even many PowerShell cmdlets have a ComputerName parameter to run on remote computers.

If you want to bring the device from Windows 10 to sleep without a waiting period of time, you can use the PowerShell sleep command.

At first, it is worth explaining that sleep mode is a low-power state in which the system suspends processes and activity and turns off the display in order to save energy as much as possible. This is a kind of alternative to shutting down, but with the ability to quickly wake up a computer and continue working at the point where you left off.

In fact, there are several types of sleep mode:

  • Sleep mode - the system and its processes are paused, the screen goes out and Windows goes into sleep. When you turn off or jump electricity - all active processes and open files will be lost.

  • Hibernation - processes and open applications are saved to the hyberfil.sys file, the screen goes blank and the device goes into hibernation, in which, if there is a power jump or a sudden shutdown, all files will be saved when you turn it on again. Hibernation is used mainly for laptops, therefore, when the battery is completely discharged, Windows goes into hibernation and restores work from the same place when charging is connected (if the corresponding item is enabled in the settings).

But there are also questions - hybrid sleep mode in Windows 10, what is it? In fact, this is hibernation, but only for stationary computers. It combines saving all active applications to a file, as in hibernation and fast waking up, as in sleep mode. In Windows 10, this term is not used and is referred to as hibernation, both on stationary PCs and laptops.

Under the concept of sleep mode in Windows 10, we will perceive all 3 types (sleep, hibernation, and hybrid), since they have common settings and work according to a general principle.

Most of the PowerShell cmdlets are dealing with automation of servers and oddball functions like sleeping a laptop normally would not be a top priority of the Windows Management Foundation / PowerShell group. Enough said... So, this guide shows you how to sleep remote computers with the help of PowerShell.



1. First Step Is to Open Elevated PowerShell Prompt


  • Recently, Windows 10 users who have encountered some kind of problems could run a particular command in PowerShell to solve these issues. While most of us are familiar with the command line and know how to run the command line as an administrator, not many know how to open the extended PowerShell line. So, today I will show you how to open PowerShell with elevated rights and run it as an administrator in Windows 10.
  • To open an elevated PowerShell prompt, in the taskbar search, type PowerShell.
  • Now see the result Windows PowerShell which appears on the top. Right-click on it and select Run as Administrator.

    Step 1 to use powershell sleep command is to Run PowerShell as Administrator


  • Then you will see a confirmation prompt from UAC. Click Yes, and PowerShell will open.

    Step 2 to use powershell sleep command is to click yes on UAC prompt


  • An elevated PowerShell prompt will display Administrator: Windows PowerShell on the top in the prompt’s border.

    elevated PowerShell prompt


  • NOTES:

  • You can also open Task Manager > File menu > Run new task. Type PowerShell and select the Create this task with administrative privileges checkbox and click OK to open an elevated PowerShell prompt.

    Run new task Powershell


  • Press Shift+Ctrl+Alt and then click on the PowerShell icon to open PowerShell as administrator.

Next Step to use powershell sleep command is to open elevated PowerShell prompt

The next step will describe how to use PowerShell sleep command or rather the script.



2. Use This Script to Sleep Remote Computer


PowerShell offers both a console interface and a full-fledged PowerShell ISE (Integrated Scripting Environment, built-in scripting environment) development environment for scripts. In the previous step, we examined how to start the console interface, so we will use it.

Scripts are saved as files with the extension .ps1. Despite the fact that PowerShell has long been a native part of Windows, you cannot run its scripts with a simple double-click. To do this, right-click on the script and select "Run with PowerShell."

There are also system policies that limit the execution of scripts. You can check the current policy settings by issuing the Get-ExecutionPolicy command. The result will be one of the following values:

  • Restricted - scripting is prohibited. Standard configuration;
  • AllSigned - you can run scripts signed by a trusted developer; Before running the script, PowerShell will ask you for confirmation;
  • RemoteSigned - you can run your own scripts or those signed by a trusted developer;
  • Unrestricted - you can run any scripts.
To get started, you need to change the startup policy setting to RemoteSigned using the Set-ExecutionPolicy command:

Next Step to use powershell sleep command is to use Set-ExecutionPolicy command

Further, you will need to save the script code in a file with the extension .ps1 and execute it in PowerShell with an elevated privileges.

  • Set-StrictMode –Version latest
  • Function Sleep-Computer {
  • #
  • .SYNOPSIS
  • Place your computer into Sleep mode
  • .DESCRIPTION
  • Since there is not a cmdlet that will 'sleep' your computer, the Sleep-Computer function was born. You can use
  • the force option to override/clear your hibernate setting and then sleep if you need to...
  • .EXAMPLE
  • c:\PS>Sleep-Computer -WhatIf
  • What if: Performing operation "Sleep-Computer" on Target Computer.
  • .EXAMPLE
  • c:\PS>Sleep-Computer -Confirm -Force
  • Confirm
  • Are you sure you want to perform this action?
  • Performing operation "Sleep-Computer" on Target "V-RONEES-W8MBP".
  • [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
  • #>
  • [cmdletbinding(SupportsShouldProcess=$True,ConfirmImpact="Low")]
  • param (
  • [parameter(mandatory=$false)][switch]$Force
  • )
  • if ($pscmdlet.ShouldProcess($env:COMPUTERNAME)) {
  • if ($Force.IsPresent) {
  • if ($pscmdlet.ShouldProcess("Turning off hibernation")) {
  • & powercfg -hibernate off
  • }
  • }
  • $rundll = Join-Path -Path ($env:windir) -ChildPath "System32\rundll32.exe"
  • & ($rundll) powrprof.dll,SetSuspendState 0,1,0
  • }
  • }



Consider using Action1 to sleep remote computer if:


  • - You need to perform this action on multiple (hundreds or even thousands) computers simultaneously.
  • - You have remote employees with computers not connected to your corporate network at all times.

Action1 is a cloud-based platform for patch management, software deployment, software/hardware inventory, endpoint management and endpoint configuration reporting. It is free with basic functionality.

  

Fully functional free edition for up to 10 endpoints with no expiration date. More details >



Relevant How To Articles and Action1 Features:

By continuing to use our website, you agree with our use of cookies in accordance with our Privacy Policy. You can reject cookies by changing your browser settings.     
Ok, got it