Thursday, July 7, 2016

One-liner to check when your Windows computer shuts down or goes to sleep

Once in a while you want to know when your computer shuts down or goes to sleep whithout watching him all the time. For example when you want to measure how long the batteries of your laptop or still hold.

A simple solution is to log the current timestamp in a file. The last timestamp is the one when the computer shut down. This is a simple PowerShell one-liner to do that:
echo "Start" > test; while (1) { date >> test ; sleep 60 }

It writes a timestamp to the file test (in the current folder) every minute until the program is stopped. If you don't stop it, then the OS does when it shuts down.