PowerShell: Showing the Uptime of all Windows Servers

Let’s say you want to get an overview of the uptime of all Windows servers in your domain. This is a very important information and you should have a look at the uptime of your services on a regular basis. Do you remember the 5 nines? What I’m talking about is the 99,999 % uptime of services. That value is well known as a baseline for good services.

The Goal

I like it to start with the goal first. The reader then has the chance to see what the goal is and to decide if it’s worth to read further on. As promised, I will give you a Powershell command in hand that gets all uptimes from all enabled domain joined Windows Servers. I’ve created a small function without any parameters, called Get-UpTimeAllServer.

The output will look like this:


If one of your servers cannot be reached due to network failure, missing WinRM configuration or for any other reason, then the server will be tagged as “Server could not be reached”.


The Function

All the shown above is wrapped into a function. Here it is:

function Get-UpTimeAllServer {

# Get-UpTimeAllServer is an advanced Powershell function. It shows the uptime of all domain joined and enabled Windows servers.

# Uses Get-CimInstance and a Try/Catch block.

# None

# Get-UpTimeAllServer

# Author: Patrick Gruenauer
# Web: https://sid-500.com

$servers=(Get-ADComputer -Filter 'operatingsystem -like "*server*"-and enabled -eq "true"').Name

Foreach ($s in $servers) {

Try {

$up=(Get-CimInstance -ClassName win32_operatingsystem -ComputerName $s -ErrorAction Stop).LastBootUpTime
$uptime=((Get-Date) - $up)
$result+=New-Object -TypeName PSObject -Property ([ordered]@{
Catch {

$result+=New-Object -TypeName PSObject -Property ([ordered]@{
'LastBootUpTime'='Server could not be reached'


You can download the function here:


Run this script in PowerShell or copy it to

C:\Program Files\WindowsPowerShell\Modules

Create a Get-UpTimeAllServer folder there and save the downloaded file as a PSM1 file. Then the command is available every time you start PowerShell.


See you next time with some cool Powershell stuff!

13 replies »

  1. Ok I’m lost 🙂
    I’ve followed the steps exactly, and I’m running the script from a domain controller, of which I’m a domain admin.
    The command just executes without any returning anything.
    I’ve also copied the script to the mentioned folder C:\Program Files\WindowsPowerShell\Modules etc.

    What am I missing?


  2. But this script gives the uptime from when the server is running continuously, if it has been restarted it does not calculate the data before restarting, right? Is there a way to get the monthly uptime and downtime of the servers?


  3. This doesn’t work. I don’t know if I’m supposed to import certain modules first or what, but it just executes, and then I’m sitting at C:\> _ . No output is displayed. Do I need to run it as domain admin? From a DC? Elevated rights?


  4. Hello Patrick!
    Awesome script. I want to use this in my AD. I do not need all servers to be checked but just a few ones of certain branchoffices which are splitted in several OUs. How does the script has to be modified to get this done?

    Thank you for your help and best regards


  5. Hello Patrick, here is again. Tell me please explaint in your script what means $result=@()

    Dose mena that variabla $result is ste as array. In your script array of variable computers



  6. Hi Patrick,
    The function is very good, but it’s only for one domain. When you have several domains without a trust relationship from which you must recover the information, you must add the script that asks for the authentication of each domain so that this function works correctly.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.