Getting Computer Uptime Using PowerShell

Getting Computer Uptime Using PowerShell

Computer uptime is an important statistic in systems management. Here are several of the ways we can determine system uptime for a computer (note that this list is by no means exhaustive):

1. The Task Manager’s Performance tab displays the computer uptime as days, hours, minutes and seconds.

2. The Systeminfo command-line tool displays the computer’s last boot time.

3. The most recent event ID 6005 in the computer’s System event log records the computer’s last boot time.

4. The WMI Win32_OperatingSystem class has a LastBootUpTime property that contains the computer’s last boot time.

Each of these techniques has their pros and cons. For example, the Task Manager provides quick visibility for a single computer’s uptime, and the event log contains additional information around the 6005 event that may provide insight about a system failure. The first three choices are probably not the best for automation purposes (e.g., querying a list of servers for uptime), so let’s look to WMI. First, though, we need to take a look at how WMI returns uptime information.

Using WMI to Get Computer Uptime

There are several ways we can retrieve a computer’s uptime using WMI. One of the most obvious ways, prior to Windows PowerShell, was to use the WMIC command. For example:

wmic path Win32_OperatingSystem get LastBootUpTime

This command retrieves the LastBootUpTime property of the Win32_OperatingSystem class instance on the computer. In PowerShell, we would use the Get-WmiObject cmdlet rather than WMIC:

Get-WmiObject Win32_OperatingSystem | Select-Object LastBootUpTime

Try these commands yourself, and you will see that the output is not necessarily “user-friendly” because the date and time expressed in the LastBootUpTime property is formatted as a CIM (Common Information Model) datetime string. For example:


This datetime string translates to May 12, 2016, 15:48:36 (3:48pm). 125599 is the number of microseconds (we’ll ignore those), and the -360 represents the number of minutes offset from GMT. In this example, -360 means the time is 6 hours behind GMT. (Similarly, +180 would mean “3 hours ahead of GMT.”)

To make the CIM datetime string more usable, we will need to convert it into a usable date (a DateTime object). Fortunately, PowerShell makes this simple: The ToDateTime static method of the  System.Management.ManagementDateTimeConverter .NET class converts the CIM datetime string to a DateTime object we can use more easily in PowerShell. Figure 1 shows an example.

Figure 1 - Converting the computer’s last boot time into a DateTime

In Figure 1, the $dateTime variable contains the date and time the current computer was last booted up. To get output of the uptime (rather than the date and time of the last boot), we can subtract it from the current time (Get-Date), which results in a TimeSpan object containing the difference. Figure 2 shows an example of this.

Figure 2 - Converting the computer’s last boot time into a readable uptime


The first command in Figure 2 creates a TimeSpan object containing the difference between the current date and time and the last bootup time (the $dateTime object we created in Figure 1). The second command outputs the TimeSpan object, and the final command uses the -f operator to output the TimeSpan as a readable formatted string that tells us the current computer has been up for 4 days, 19 hours, 56 minutes, and 44 seconds.

Getting the Last Boot Time from a Remote Computer

The examples in Figures 1 and 2 target the local computer. How can we target a remote computer? Fortunately WMI allows us to retrieve information about a remote computer by using the -ComputerName parameter. WMI will also allow us to specify alternate credentials for a remote computer in a PSCredential object. Figure 3 shows an example of getting the last bootup time for a computer named server1.

Figure 3 - Getting the last boot time from a remote computer

The first command in Figure 3 gets a PSCredential object into the $cred variable, and the second command gets the CIM datetime string containing the last boot time for the computer server1 (notice the -ComputerName and -Credential parameters). The third command converts the last boot time into a DateTime object, and the last command outputs the last boot time for the computer.

Putting It All Together - the Get-Uptime.ps1 Script

The examples shown in Figures 1 and 2 show how to get the computer’s last boot time and show as an easy-to-read string, and Figure 3 shows how to get a remote computer’s last boot time. Of course, there’s no need to remember all of the nuances if we encapsulate the logic into an easy-to-use script. The Get-Uptime.ps1 script does all the hard work for us. The script’s syntax is as follows:

Get-Uptime [-ComputerName [] [[-Credential] ]

The -ComputerName parameter is optional and names the computer(s) for which you want to get the uptime information. This parameter supports multiple objects, pipeline input, and objects that have a ComputerName property. The -ComputerName parameter name is optional. If you omit this parameter, the default is to output uptime information for the current computer.

The -Credential parameter supplies alternate credentials if you want to query one or more remote computers and your current logged-on account does not have access. Querying uptime does not require administrator privileges on the local computer, but it is required for remote computers.

The script outputs objects with three properties: ComputerName (the computer’s name), LastBootTime (a DateTime containing the computer’s last bootup time), and Uptime (an easy-to-read uptime string).

Getting Uptime for Multiple Computers

Figure 4 illustrates getting the uptime for three remote computers two different ways.

Figure 4 - Getting the uptime for three remote computers two different ways

The first command in Figure 4 specifies the three computers as a parameter to the Get-Uptime script, and the second command specifies the three computers as pipeline input. Note that both commands produce identical output (except that the uptime strings in the output from the second command are slightly later).

Because the script supports pipeline input, you can even use the output of the Get-ADComputer cmdlet to report on the uptime for all computers in an OU. For example, consider the following command:

Get-ADComputer -Filter { Name -like "*" } `
  -SearchBase "OU=Servers,DC=fabrikam,DC=com" |
  Select-Object -ExpandProperty Name | Sort-Object |

(I split the command onto multiple lines to make it easier to read.) This command retrieve the names of all computers in the Servers OU, selects only the Name property of each computer (Select-Object -ExpandProperty), sorts the list of computer names, and finally gets the uptime from each computer.

Computer Uptime Made Easy

Computer uptime is an important metric. There are a variety of ways to get uptime but not a simple way in PowerShell. The Get-Uptime.ps1 script fills in this gap and makes it easy to get computer uptime, no matter whether you want to get the current computer’s uptime or uptime for remote computers.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.