How to Check Your Windows Server Uptime with PowerShell

An image of text showing the word uptime
Uptime is a key metric for server management success.

When you talk about hardware, you’re talking about computers, keyboards, switches, cables, and the list goes on. Only, the most important component for connectivity is a server! 

Servers, centralized or off-site, serve as intermediaries for information transmission and hence form a critical part of operations.  That’s why an organization’s infrastructure team will often have dedicated daily or weekly tasks  to continuously monitor server performance through many metrics.

One key metric is uptime, which denotes the time a system has run without a restart or a shutdown. You wouldn’t want the system to fail you in the middle of a tough number crunch. An IT infrastructure team can quickly determine the health and wellbeing of its system and applications and correct for unexpected downtime. 

Time is money, so the shorter the diagnostics take, the quicker you can fix any problems and ensure your company’s working efficiently. 

That’s what Microsoft’s PowerShell utility achieves and here’s how. First, let’s check why uptime is such an important metric.

Why Should You Care about Uptime?

It’s because the inverse, downtime, is devastating–both in terms of cost and reputation! High uptime rates increase a system’s reliability and availability and help provide uninterrupted service to your customers. 

Identifying the reasons for downtime can also help with decision-making, budgeting, and capacity planning.

Decision making: As a server administrator, you’ll know which systems need an upgrade to meet your organization’s operations. Accordingly, you can recommend the next course of action to your superiors.

Budgeting and capacity planning:  Based on uptime, you and your team can decide if more resources are needed to handle the growing needs of the organization or if any machines need a replacement. Accordingly,  decide on the budgets and capacity planning. 

Since uptime affects your entire business model, let’s dive deeper into Microsoft’s PowerShell utility and all the exciting benefits it offers.

What is Microsoft PowerShell?

Before PowerShell, Microsoft had just one command line interface (CLI) you could use to interact with Windows OS and that was CMD. When Microsoft introduced PowerShell, it became a gamechanger for automating repetitive tasks and improving administrative task management. PowerShell is easy to program with and deploy, and is a powerful way to interact with Windows OS

Now, let’s see how easy it is to use PowerShell to check a system’s uptime!

4 Ways to Calculate Server Uptime with PowerShell

First things first– you need to understand what Windows Management Instrumentation (WMI) and Common Information Model (CIM) are. 

WMI consists of many extensions that provide you with information about the operating system. CIM provides a common definition of management information, including a device’s properties and applications within a system. It’s like an organizational tool that allows IT to know everything about the infrastructure and find it easily. 

How are these two related? WMI is Microsoft’s implementation of CIM on the Windows platform. In other words, you access CIM information, like uptime, through WMI

Now that we’re done with definitions, let’s check the 4 ways to get your server’s uptime using Microsoft’s PowerShell.

1. Querying Windows Management Instrumentation (WMI)

One way you can find a Windows server’s uptime is by querying the WMI. 

The WMI extension for calculating uptime is a class called Wi32_OperatingSystem, and it comes with many properties. The LastBootUpTime property tells you when the last computer reboot happened. You also need the CIM information for this and you’re set. 

Query PowerShell with the following:

Get-CimInstance -ClassName Win32_OperatingSystem | Select LastBootUpTime

The output is always in the Date-Time format, which you can use to calculate the uptime.

 A query of Get-CimInstance -ClassName Win32_OperatingSystem | Select LastBootUpTime shows the last boot up time as 7:19:28 am on January12, 2022.
Rise and shine! I guess the server needed some coffee.

2. Windows Event Log

A second option you might use is the Windows Event Log. You can access this in PowerShell through Get-EventLog or Get-WinEvent command. Check out our list of all basic PowerShell commands for more details. These commands gather information from event logs and event tracing log files located on local and remote computers. Instead of going through the complete log file, you can simply choose the metrics you want to see which have individual IDs.

For example, let’s say the event is 1002 or 1003 for the last restart time. Using either ID, you can know the last time the system restarted. From that, you can calculate the uptime.

The PowerShell code for finding the last restart time is:

Get-WinEvent -ProviderName EventLog | Where-Object {$_.Id -eq 1002 -or $_.Id -eq 1003} | Select-Object -First 1 TimeCreated

Here, you’re asking PowerShell to fetch details for event ID 1002 or 1003 from the event log. The output will be the last restart time.

The query Get-WinEvent -ProviderName EventLog | Where-Object {$.Id -eq 1002 -or $.Id -eq 1003} | Select-Object -First 1 TimeCreated gives the result 7:20:14 am on January12, 2022.
I didn’t get an invite to this event! Feeling left out!

Before we move to the next option, let’s quickly look at some important parameters you can use with the Get-WinEvent cmdlet:

  • ComputerName – defines computer’s name from which this cmdlet gets system logs
  • Credential – specifies a user account with the permissions to do this action
  • FilterHashtable – takes a query in hashtable form
  • FIlterXML – specifies a structured XML query
  • Force – fetches a debug log when the computer name has wildcard characters
  • ListLog – specifies the event logs
  • LogName – gives the specific log name

You can use these parameters to get any Windows server or servers’ uptime.

3. Using Get-Uptime

If the above options seem arduous, here’s an easy one. Simply use the Get-Uptime cmdlet to get the time elapsed since the last reboot. This cmdlet’s obvious advantage is you don’t have to calculate anything since it directly gives your server’s uptime as the output.

Note that this cmdlet was introduced in PowerShell 6.0, so if you have an older version it won’t work for you. 

This cmdlet takes two parameters, namely:

Get-Uptime

[-Since]

[<CommonParameters>]

The [-Since] cmdlet returns a DateTime object to give you the server’s last restart date and time. If you use the cmdlet without any parameters, it only returns the uptime. 

Here’s an infographic to show you the difference in output results. 

Examples

Get-Uptime -Since

Output – Tuesday, Jan 4, 2021 1:13:49 PM

As you can see, this cmdlet displays the last time the system was restarted. Now, if you want just the uptime, here’s the code for it.

Get-Uptime

The output will be:

Days              : 10

Hours             : 4

Minutes           : 56

Seconds           : 32

Milliseconds      : 0

Ticks             : 8892960000000

TotalDays         : 10.03677083333333

TotalHours        : 83.2736

TotalMinutes      : 23070.96

TotalSeconds      : 997364

TotalMilliseconds : 889296000

This information is the time since the last reboot; much simpler than calculating it, right! You can parse this output in your code or take any other action based on it.

4. Custom Scripts

While the above cmdlets are built-in functions from PowerShell, you can also create your own script or use custom scripts the PowerShell community members and groups develop. An example is called Get -ServerUptimeReport.

As the name suggests, this script takes a computer’s name as its parameter and parses the computer’s system event log to find the last start time. It then calculates the total time the server was up and running. This cmdlet also gives a historical account of the computer’s start and stop times. 

You can use this cmdlet as follows:

./Get-ServerUptimeReport.ps1 -ComputerName comp1

The output will be: 

1/2/2021 3:15:19 PM 20.31 9286.98

These scripts’ advantage is that you can find the uptime of many Windows servers with the same command since this script takes an array. For example, you can have an array of computer names and pass this array as input. Just make sure you type the computer name along with uptime for clarity.

You can also customize this script to meet your specific requirements. Alternatively, you can build one from scratch! 

The Final Words

Uptime measures a system’s reliability and availability, which is vital to maintain efficiency. Microsoft PowerShell helps you find the uptime of a Windows server and you can achieve this using 4 different ways. 

You have the option to query the WMI using the CIM information. Alternatively, you can use built-in PowerShell cmdlets, like Get-WinEvent or Get-Uptime, or choose to build your custom script to fetch the information. You also can use community-built PowerShell scripts for custom inputs and outputs.

In all, it depends on what you want! No matter which option you choose, PowerShell offers quick results for your system’s uptime metrics

FAQs

 

Which is better, Get-WinEvent or Get-EventLog?

You can use both Get-WinEvent and Get-EventLog for accessing Windows event logs. They’re similar in many ways, though Get-WinEvent is more advanced than Get-EventLog. The main difference is that Get-WinEvent works well with newer Microsoft log technology versions. This command can also access more data than Get-EventLog.

What’s CIM and WMI?

The Common Information Model (CIM) is an open-source standard for collecting and displaying information about a computer. Windows Management Interface (WMI) is Microsoft’s implementation of CIM for the Windows operating system.

What’s the difference between uptime and availability?

Uptime is the time elapsed since the last restart and measures a system’s reliability. Availability is the probability that a system will be up and running during a specific time. Bottom line, uptime is actual data while availability is an expectation. 

What are custom scripts in PowerShell?

Custom scripts are the code you write in PowerShell to get a specific output. You can use the existing commands in PowerShell to create a script that does a desired action. 

Resources

 

TechGenix’s PowerShell Progress Bar Blog

To progress your PowerShell Kungfu, take a look at this exciting blog on adding progress bars.

TechGenix’s PowerShell Battery Check Blog

Think your laptop is lying about it’s battery or just curious about finding a quick way to check it, have a look at this PowerShell Battery Check blog.

TechGenix’s Tutorial to Schedule PowerShell Scripts

Get your scripts rolling with our tutorial on scheduling PowerShell scripts

Microsoft’s Powershell Documentation

For more information on PowerShell’s Get-WinEvent and Get-CimInstance scripting commands, take a look at Microsoft’s reference documentation. 

Hyper-V Virtual Machine Preparation for Windows 11

How can you prep your machine for Windows 11? Check our PowerShell guide to get you machine ready fast.

Leave a Comment

Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top