I usually put my computer to sleep when I am done, so that it can start quickly the next time I am on it. It was annoying that I had to click a few times before I can select the Sleep option. I watned a single click solution, where I press an icon docked on my takbar, and it runs the sleep command. Following works on a Windows 7 computer.
Following is the powershell script (original source) for making computer to sleep:
Add-Type -Assembly System.Windows.Forms
[System.Windows.Forms.Application]::SetSuspendState("Suspend", $false, $true)
Open notepad and copy the above text. Save it as Sleep.ps1 somewhere on your Desktop.
Note: If you right click on the file and choose "Run with PowerShell", it will put your computer to sleep. But that's two clicks and we want a single-click solution!
Run PowerShell as administrator.
Run the following command to download and install it from the PowerShell Gallery:
Install-Module ps2exe
Note: when trying the above command, it asked for NuGet provider and threw an error that it was unable to download from URI...
This error had something to do with the security protocol (original source).
Check current security protocol:
[Net.ServicePointManager]::SecurityProtocol
, which showed Ssl3, Tls
We need Tls12. To do this, run in PS
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Run the previous command and you should see Tls12 instead of Ssl3, Tls
Run the Install-Module ps2exe
command and it should work.
- Download a graphical user interface utility called Win-PS2EXE.exe file from the MScholtes/PS2EXE GitHub page.
- Download an appropriate "Sleeping" icon file (.ico) from the iconarchive website.
- Run the Win-PS2EXE.exe GUI. Select the Sleep.ps1 file from step 1 as source, select Desktop as the target and select the icon file.
Check options for "suppress output" and "suppress error output" and it will generate a Sleep.exe file on the Desktop. - Dock it to your Taskbar by dragging it over. Click on it to put your computer to sleep with a single click Zzzz...
Many errors were encountered on the way. Here is how I fixed them:
-
To execute PS scripts, changed the Execution policy using PowerShell
Get-ExecutionPolicy
showed Restricted, which means all scripts are blocked from running. Changed this to RemoteSigned by running:Set-ExecutionPolicy RemoteSigned
-
After sucessfully trying out the method here, execution policy could be set back to Restricted in PS, without affecting the single-click sleep.
-
Before running step 2, I needed to upgrade my PowerShell, which was version 2, to version 5.1 (Instructions here).
First, check the current PS version with$PSVersionTable
or$PSVersionTable.PSVersion
orGet-Host | Select-Object Version
Download the latest Windows Management Framework from the Microsoft site - Win7AndW2K8R2-KB3191566-x64.zip, which is for a 64 bit computer. Unzip it and you will see the following two files:
Run the powershell script Install-WMF5.1.ps1 file on PS prompt.