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.
This example code runs well under PS version 5.1 but not under 7.0.2. Apparently there are either differences in cmdlets and Modules in the latest 7 release. Here is the error that I received
:
InvalidOperation: C:\Temp\Getting Computer Uptime Using PowerShell-Example-02.ps1:28
Line |
28 | $sysuptime= (Get-Date) – $userSystem.ConvertToDateTime($userSystem.La .
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Method invocation failed because
| [Deserialized.System.Management.ManagementObject#root\cimv2\Win32_Operati
ngSystem] does not contain a method named ‘ConvertToDateTime’.
InvalidOperation: C:\Temp\Getting Computer Uptime Using PowerShell-Example-02.ps1:29
Line |
29 | Write-Output (“Last boot: ” + $userSystem.ConvertToDateTime($userSyst .
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Method invocation failed because
| [Deserialized.System.Management.ManagementObject#root\cimv2\Win32_Operati
ngSystem] does not contain a method named ‘ConvertToDateTime’.
PS> Uptime : Days Hours Minutes