Monitoring the Computer up-time is an important statistic in systems management.There are several ways we can retrieve a computer’s uptime using WMI (Windows Management Instrumentation) . PowerShell provides an easy way to accomplish this with the Get-WMIObject commandlet, In this post we will discuss how you can get both the last boot time as well as the current up time for a computer.
Step #1: The first step you need get the required WMI Object Class and its property for the last boot-up time. Finally assing the value to variable
$userSystem=Get-WmiObject win32_operatingsystem -ComputerName $ComputerName -ErrorAction SilentlyContinue
Step #2: The second step is to convert the property of LastBootUpTime to a Date and Time object. If you try $userSystem.LastBootUpTime commands then you will see that the output is not an user-friendly because the date and time expressed in the property is formatted as a CIM (Common Information Model) date time string. So its necessary to user conversion to see friendly format.
#Getting Last Boot time
$userSystem.ConvertToDateTime($userSystem.LastBootUpTime)
Step #3: We you can determain and print the Last Boot Time
Write-Output (“Last boot: ” + $userSystem.ConvertToDateTime($userSystem.LastBootUpTime) )
Write-Output (“Uptime : ” + $sysuptime.Days + ” Days ” + $sysuptime.Hours + ” Hours ” + $sysuptime.Minutes + ” Minutes” )
Final Code
1 2 3 4 5 6 7 8 9 10 |
$ComputerName = $env:COMPUTERNAME $userSystem = Get-WmiObject win32_operatingsystem -ComputerName $ComputerName -ErrorAction SilentlyContinue if ($userSystem.LastBootUpTime) { $sysuptime= (Get-Date) - $userSystem.ConvertToDateTime($userSystem.LastBootUpTime) Write-Output ("Last boot: " + $userSystem.ConvertToDateTime($userSystem.LastBootUpTime) ) Write-Output ("Uptime : " + $sysuptime.Days + " Days " + $sysuptime.Hours + " Hours " + $sysuptime.Minutes + " Minutes" ) } else { Write-Warning "Unable to connect to $computername" } |
OUTPUT
What do you think?
I hope you have an idea of how to Getting Computer Up-time Using PowerShell. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.