You want to get an out-of-the-box solution to find out the uptime of your machine since the last hybrid shutdown / fast startup took place, right?
You can get this information (as provided by @allquixotic) from the EventLog using PowerShell like this:
PS c:\> Write-Host $("{0:c}" -f ((Get-Date)- (Get-EventLog -LogName system -Source "Microsoft-Windows-Power-Troubleshooter" -Newest 1).TimeGenerated))
To embed powershell command into a Windows shell script you can do this instead:
c:\> powershell.exe -nologo -command Write-Host $('Time since last ''Fast Startup'': {0:c}' -f ((Get-Date)- (Get-EventLog -LogName system -Source \"Microsoft-Windows-Power-Troubleshooter\" -Newest 1).TimeGenerated))
Howerver, to get this to work out of the box, you could set it into a permanent environment variable like this instead:
c:\> setx HardwareUptime "powershell.exe -nologo -command Write-Host $('Uptime since last ''Fast Startup'': {0:c}' -f ((Get-Date)- (Get-EventLog -LogName system -Source 'Microsoft-Windows-Power-Troubleshooter' -Newest 1).TimeGenerated))"
so, you can then get it to work by opening up a cmd
window and doing:
c:\> %HardwareUpTime%
UPDATE:
I just found today that using the above entry in the event log will also take into account "Sleep" or suspend mode, so running %HardwareUpTime%
will tell you the elapsed time since the PC resumed from sleeping if you let it do so.
Therefore, here it is:
setx HardwareUptime "powershell.exe -nologo -command Write-Host $('Uptime since hardware boot: {0:c}' -f ((Get-Date)- (Get-EventLog -LogName system -InstanceId 27 -Newest 1).TimeGenerated)); Write-Host $('Uptime since system resumed: {0:c}' -f ((Get-Date)- (Get-EventLog -LogName system -Source 'Microsoft-Windows-Power-Troubleshooter' -Newest 1).TimeGenerated));"
I've modified the command a little bit to be more explicit and give you both pieces of information:
Elapsed time since Windows booted (either after a hybrid shutdown, full shutdown or simple reboot or hibernation).
Elapsed time since Windows resumed execution (after returning from sleep mode).
NOTE: If the system didn't sleep in between, both times will be the same.