Soon after the publication of my last article, “Tweaking power options in Windows 8.1,” I received a couple of questions from readers concerning Window’s sleep states. One reader mentioned that she noticed that the longer her laptop is in sleep mode, the longer it takes to wake up. Another reader said that there are times when he puts his laptop into sleep mode, and it stays that way until he manually wakes it up — but there are other times when the laptop spontaneously wakes up a short time after being put into sleep mode. He wondered what was waking up his system.

The answer to both questions has to do with Windows sleep states, which I’ll describe in this article. I’ll then show you how to use the PowerCfg command to investigate Windows sleep states and the devices that support them. Finally, I’ll provide you with batch file that uses the PowerCfg command and a PowerShell script to generate and display an HTML report of the devices in your system that support Windows sleep states. You can download both here.

Sleep states

When Windows enters sleep mode, it may appear to you, as the user, that the system simply goes into a low power state and remains that way. However, the process is a bit more complex. and there are actually several levels of sleep that the system goes through. These levels, or more specifically, power states, are defined by the Advanced Configuration and Power Interface (ACPI) specification. All total, the ACPI spec lists six states and are numbered S0 through S5. While we are mainly concerned about the sleep states, to keep everything in context, let’s look at them all.

S0

At S0, the computer is in a Working power state, and the system is fully operational. Even so, certain devices that are not currently being used can enter into a low-power state to save power.

S1

When a computer first enters sleep mode, S1 is the first level of sleep. At this level, the processor clocks are stopped to save power. Memory still has power and maintains its content. When awakened, the system will typically return to S0 in less than two seconds.

S2

After a period of time in S1, the computer enters S2, the second level of sleep. At this level, the processor shuts off to save power. Memory still has power and maintains its content. When awakened, the system will typically return to S0 between two and five seconds. Keep in mind that the amount of time can vary, depending on your hardware.

S3

After a period of time in S2, the computer enters S3, the third level of sleep. At this level, in addition to the processor, other chips on the motherboard may shut off to save power. Memory still has power and maintains its content. When awakened, the system will typically return to S0 between five and eight seconds. Keep in mind that the amount of time can vary, depending on your hardware.

S4

S4 is hibernation. At this level, an image of the contents of memory is written to disk and power to all devices is turned off. However, a small trickle of power to the power button remains. When awakened, the amount of time that it takes the system to return to S0 depends on many factors. However, it will definitely take longer than S1-S3.

S5

At S5, the computer is shut down. However, a small trickle of power to the power button remains. When the power button is pressed, the computer performs a cold boot in order to return to S0.

Note: Keep in mind that if a computer in states S1, S2, or S3 loses all battery power, it must reboot to return to S0. As such, anything in memory is lost. However, if a computer in state S4 loses all battery power, it will still restart from its previous location, because the contents of memory are retained in the hibernate file.

While the ACPI specs deal with the main system components, many devices also adhere to the six states. You can investigate the sleep states of the devices attached to your system with the PowerCfg command.

The PowerCfg command

Among all the powerful parameters that the PowerCfg command provides is the DeviceQuery parameter and its 11 QueryFlags. To see all of these QueryFlags, you can open a Command Prompt window and type the command:

powercfg /devicequery /?

When you do, you’ll see a list of flags and descriptions as shown in Table A.

Table A

A list of flags and descriptions.

As you can see, these flags will allow you to find out what devices in your system support states S1 through S4 and what devices support waking the system from states S1 through S3. You can also find out what devices in your system are currently configured to wake the system from any sleep state.

For example, typing the command:

powercfg /devicequery wake_programmable

will show you all of the devices on your system that you can configure to wake the system from a sleep state.

The batch file

While the PowerCfg command, along with the DeviceQuery parameter and its QueryFlags, can provide you with good information that you can use to investigate and troubleshoot the sleep states, gathering that information from a Command Prompt is a tedious operation. To make the process easier, I’ve created a batch file that runs the powercfg /devicequery command with 10 of its 11 flags. I left out the all_devices flag, since it lists all devices present in the system, regardless of sleep state information. The batch file uses redirection to send the output of each of the powercfg /devicequery commands to a text file. Each command is separated by a header that identifies the flag generating the results.

Since the batch file is pretty old school and straightforward, I won’t go into any more detail. However, I will add that it produces a fairly nice report in and of itself. Still, having the report in a text file felt pretty archaic, so I set out to find a way to get the report into an HTML format using PowerShell.

The PowerShell script

Since I had the report in a text file, my first thought was to simply create a PowerShell script that uses the ConvertTo-Html cmdlet to convert the text file into an HTML document. However, I soon discovered that wouldn’t work, as the ConvertTo-Html cmdlet is designed to convert objects, not text strings, into HTML. Figuring that I wouldn’t be the only one who wanted to use PowerShell to convert a text file into an HTML document, I turned to Google.

Soon, I discovered a little script segment posted by newlife007 over at Life of a Geek Admin that did exactly what I needed after a few slight modifications. The key to the process is to use the Add-Member cmdlet to convert each line of text into a Windows PowerShell object. With each line in the text file now an object, the ConvertTo-Html cmdlet produces a great HTML-formatted report (Figure A). Again, the PowerShell script is pretty straightforward, so I won’t go into any more detail.

Figure A

The batch file and PowerShell script combination produces a great HTML formatted report of the PowerCfg command’s results.

Using the batch file

Once you download the batch file and PowerShell script (pdq.bat and reportit.ps1) you need to copy them into a folder on your hard disk. You’ll then need to make a slight modification to both the batch file and PowerShell script to specify the path of the folder into which you copied them.

For example, suppose that you copied the batch file and PowerShell script into the C:\scripts folder on your hard disk. If so, then you need to open the reportit.ps1 PowerShell script in a text editor and edit the first two lines so that the path to the source and target files is correct:

$SourceFile = “C:\scripts\devicequery.txt”

$TargetFile = “C:\scripts\devicequery.html”

You then need to open the pdq.bat batch file in a text editor and edit the last line so that the path to the reportit.ps1 file is correct:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command “& ‘C:\Scripts\reportit.ps1′”

Once you’ve edited and saved the batch file and PowerShell script, all you have to do to get your report is run the batch file. Just open a Command Prompt window in the C:\Scripts folder and type pdq at the prompt.

What’s your take?

Have you ever wondered about the details of the devices on your system that can wake your system from a sleep state? If so, download and run the batch file to learn more. Share your thoughts about this topic in the discussion thread below.

Subscribe to the Microsoft Weekly Newsletter

Be your company's Microsoft insider by reading these Windows and Office tips, tricks, and cheat sheets. Delivered Mondays and Wednesdays

Subscribe to the Microsoft Weekly Newsletter

Be your company's Microsoft insider by reading these Windows and Office tips, tricks, and cheat sheets. Delivered Mondays and Wednesdays