Powershell

PowerShell Get Uptime

The server plays a vital role as an intermediary for information transmission, off-site or centralized. An organization’s infrastructure team often dedicates weekly or daily tasks to monitor server performance through multiple metrics. A key metric, uptime, refers to the time taken by the system to run without shutdown or restart.

Sometimes, you need to know about your system uptime or servers in your day-to-day operations. This article will discuss different ways to get Linux and Windows operating systems system uptime. It will also tell you when your system last booted.

Uptime: What are the benefits?

Uptime rates are high because they improve system availability and reliability. Conversely, downtime is disastrous both in terms of reputation and cost. Understanding the causes of downtime also helps you with capacity planning, budgeting, and decision-making.

Capacity Planning and Budgeting

With the help of uptime, your team and you can decide whether a machine needs to be replaced or more resources are required to meet the organization’s growing needs. And on this basis, you can also choose capacity planning and budgeting.

Decision Making

As a server administrator, you can easily determine which systems you need to upgrade to carry out the organization’s operations. You can recommend the next course of action to your superiors.

Microsoft PowerShell

Before PowerShell, Microsoft had CMD. It was the only CLI (Command-Line Interface) at Microsoft using which you could interact with the Windows OS. PowerShell in Microsoft proved to be a game-changer as it automates repetitive tasks and improves administrative task management. With PowerShell, you can interact with Windows OS and programming.

Check Uptime in Linux System

Several commands are available in Linux for checking system uptime. Here, we will tell all the commands with the help you can check the uptime in the Linux system.

‘w’ Command

Running this command displays system uptime and logged-in user information.

‘uptime’ Command

With the help of this command, you will know how long the system has been running.

‘top’ Command

Along with showing system uptime, this command also shows you resource statistics.

Check Uptime in Windows System

In Windows, you can check the uptime in several ways. Here, we will discuss all those methods and tell you how to check uptime in Windows.

Using Command Prompt

For this method, open PowerShell or prompt to run the command “systeminfo.” Find “System Boot Time” and enter the following command to get the results.

systeminfo

You get the complete system information like ram, processor, hotfix, network, name, etc., by running the above command.

Using Task Manager

This method is one of the easiest ways to know the system’s uptime. Follow these steps to see the uptime in Windows through this.

  • Open your system and right click on the taskbar.
  • When Task Manager opens, now click on the Performance tab.
  • By doing this, you can see the system uptime in the above Windows.

Using WMIC

Using PowerShell or CMD, you can check the system’s uptime. With its help, you will get the same output.

wmic path Win32_OperatingSystem get LastBootUpTime

It’s easier to understand if you break it down.

  • Year: 2022
  • Month: 12
  • Day: 09
  • Hour: 05
  • Minute: 18
  • Second: 32
  • Millisecond: 400000

Using Net Statistics Command

You can use the net statistics command to see the uptime. Use CMD or PowerShell to run the commands listed below.

net statistics workstation or net statistics server

Using Uptime.exe Utility

The Uptime for Windows utility is a simple program. It allows you to find out when you last restarted Windows. Please place it in the system32 folder of the computer after downloading it from the NeoSmart website. Double-clicking it will produce results.

Server Uptime with PowerShell

To begin with, understand CIM (Common Information Model) and WMI (Windows Management Instrumentation). To easily find everything about IT infrastructure and get information about the infrastructure, CMI acts like an organizational tool. CIM provides a general definition of management information, including the applications and properties of devices within a system. Many such extensions within WMI give you information about the operating system.

Calculate Server Uptime with Microsoft PowerShell

Let us now see how you can calculate the server’s uptime with the help of Microsoft PowerShell using different methods.

Querying WMI (Windows Management Instrumentation)

You can find out the uptime of Windows Server by querying WMI. To calculate uptime via this, there is a class called WMI extension Wi32_OperatingSystem with many properties. Here, the LastBootUpTime property tells you in the data when was the last time you rebooted your system. You get the output by doing a PowerShell query with the following.

Get-CimInstance -ClassName Win32_OperatingSystem | Select LastBootUpTime

Here, you will always get the output in date-time format, using which you can calculate the uptime.

Windows Event Log

Windows Event Log is another option with the help of which you can check server uptime. You can use the Get-Event or Get-EventLog command to access it in PowerShell. You collect even tracing log files and event log information on the remote or local computer by viewing a list of all basic PowerShell commands for more details. For simplicity, you can select the metrics you want to see rather than having to read the entire log file.

Suppose the last restart time was 1005 or 1006. You can now find out the last time the system was restarted by using any ID. This allows you to calculate the uptime.

To find the last restart time, run the following PowerShell code:

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

In the above command, you have asked PowerShell to fetch the details of event IDs 1005 and 1006 from the Event Log.

Using Get-Uptime

Before understanding the above method, we need to know some important parameters. The Get-WinEvent cmdlet allows you to do these things.

  • ComputerName – Specifies the computer’s name from which the cmdlet gets the system logs.
  • Credential – Identifies the account authorized to act.
  • FilterHashtable – Processes queries as hashtables.
  • FIlterXML – Defines a structured XML query.
  • Force – Displays a debug log if the computer name includes wildcard characters.
  • ListLog – Displays information about the event logs.
  • LogName – Identifies the specific log name.

If you don’t understand the above option or it takes days, you can get elapsed time since the last reboot using the get-uptime cmdlet. The best advantage of this is that you get your server’s uptime directly as output when you use it, and you don’t need to calculate anything. This doesn’t work when there is a Puranic version of PowerShell.

There are two parameters in this cmdlet, namely:

Get-Uptime

[-Since]

[<CommonParameters>]

Using the [-Since] cmdlet, you can get the last date and time of the server’s restart. When using the cmdlet with no parameters, it just returns the uptime.

Custom Scripts

All the above methods have built-in functions from the PowerShell cmdlets. You can also use custom scripts or your scripts to develop PowerShell’s group and community members. Here, Get -ServerUptimeReport consumes an example.

As you can see from its name, this script takes a computer name as a parameter and parses the system event log to determine the last time you started the computer. This cmdlet calculates the total time taken when the server is up and running, and this cmdlet recounts computer pauses and starts.

Use this cmdlet:

./Get-ServerUptimeReport.ps1 -ComputerName comp1

Since this script takes an array, you can get the uptime of multiple Windows servers with a single command. You can use computer names as input when you have many. For clarity, type the computer name and the date and time. In addition to customizing this script to your specifications, you can also build one from scratch.

Conclusion

Uptime measures the availability and reliability of the system to maintain the system’s efficiency. This article taught us to calculate uptime on Linux and Windows systems through different means. We saw that with the help of Microsoft PowerShell, you could also find out the server’s uptime. Here we have also discussed four methods of calculating the Server Uptime with Microsoft while giving complete information. Through this, you can calculate the server’s uptime.

We hope you must have understood the complete information provided by us regarding uptime. We hope you will calculate the uptime on Windows / Linux systems and the server’s uptime for Windows.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.