PowerShell – getting system uptime in human readable format

In order to get the system uptime in the format No Days No Hours No Minutes we use the below script:

######################################################
#
# NAME: Get-SyatemUptime.ps1
#
# AUTHOR: Alin Daniel Stanciu
#
# COMMENT: Script to check the last update for McAfee antivirus by reading the registry
# keys and taking the last DAT version and DAT date
# In order to also check for services status uncomment last line
#
# VERSION HISTORY:
# 1.0 09 november 2017 – Initial release
# USAGE: get-systemuptime.ps1
########################################################
$OS = Get-WmiObject win32_operatingsystem
$BootTime = $OS.ConvertToDateTime($OS.LastBootUpTime)
$enddate=(Get-Date)
$diff=(NEW-TIMESPAN –Start $boottime –End $EndDate)
write-host $diff.days Days $diff.Hours Hours $diff.Minutes Minutes

The result is like this: 21 Days 21 Hours 56 Minutes.

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.