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.
1Can you clarify what you mean by "real up time"? What did you do 7 minutes ago exactly? – David Schwartz – 2013-03-11T13:07:46.363
Real up time means the actual time from when I turned on the system. (That's what I did 7 min ago, turn on the Laptop!) – Akshat Mittal – 2013-03-11T13:32:54.597
8The legacy definition for uptime is the time that the computer has been running since the kernel initialized itself. Since hybrid boot is just a special type of suspend (there was previously suspend to RAM and Hibernate), it doesn't count as "shutting down", because the same instance of the Windows kernel is used. – allquixotic – 2013-03-11T13:56:12.730
@allquixotic True, That's why I thought it would be Hybrid-Boot up time. – Akshat Mittal – 2013-03-11T14:15:13.810
I guess you din't actually got the Question. I want to ask that how to get the actual up-time even when Hybrid-Boot in enabled. I am updating the question. – Akshat Mittal – 2013-03-12T14:25:56.487