HOWTO: Powershell Script to Sleep Remote Computer


Well, there was not one. Most of the PowerShell cmdlets are dealing with automation of servers and odd ball functions like sleeping a laptop normally would not be a top priority of the Windows Management Foundation / PowerShell group. Enough said... So, this guide show you how to sleep remote computer with help of PowerShell.


1. Open PowerShell prompt:

   - 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.
The UAC prompt will ask you for your consent. Click yes, and the prompt will open.
An elevated PowerShell prompt will display Administrator: Windows PowerShell on the top in the prompt’s border.
NOTES:
You can also open Task Manager > File menu > Run new task. Type powershell and select the Create this task with administrative privileges check box and click OK to open an elevated PowerShell prompt.
Press Shift+Ctrl+Alt and then click on the PowerShell icon to open PowerShell as administrator.

Open PowerShell prompt

2. Use this script to sleep remote computer:

   - 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
}
}

Also consider using Action1 to sleep remote computer if:
- You need to perform this action on multiple (hundreds or even thousands) computers simultaneously.
- Some of your endpoints are laptops not connected to corporate network at all times.

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


Other Relevant HOWTOs: