I created a PowerShell advanced function that emulates the uptime command for Unix-like operating systems.

The uptime command displays the current time, the length of time the system has been up, the number of users, and the load average of the system over the last 1, 5, and 15 minutes.

uptime

output:

21:33  up 7 days, 11:10, 2 users, load averages: 0.05 0.08 0.08

My function returns a custom PowerShell object, so we have the option to pass it to the pipeline for further processing and/or formatting.

Get-Uptime | Get-Member

output:

TypeName: BN.Uptime

Name        MemberType   Definition
----        ----------   ----------
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
Computer    NoteProperty System.String Computer=DC01
Days        NoteProperty System.Int32 Days=9
Hours       NoteProperty System.Int32 Hours=12
Minutes     NoteProperty System.Int32 Minutes=46
Seconds     NoteProperty System.Int32 Seconds=16

Here is the function:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function Get-Uptime
{
    [CmdletBinding()]
    param
    (
        [Parameter(ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [Alias('hostname')]
        [Alias('cn')]
        [string[]]$ComputerName = $env:COMPUTERNAME
    )

    BEGIN {}

    PROCESS
    {
        foreach ($computer in $ComputerName)
        {
            try
            {
                $os = Get-WmiObject -Class Win32_OperatingSystem `
                -ComputerName $computer -ErrorAction Stop
                $time = $os.ConvertToDateTime($os.LocalDateTime) - `
                        $os.ConvertToDateTime($os.LastBootUpTime)

                # Create property hash table for custom PS object
                $props = @{'Computer'=$os.CSName;
                           'Days'=$time.Days;
                           'Hours'=$time.Hours;
                           'Minutes'=$time.Minutes;
                           'Seconds'=$time.Seconds;}

                # Create custom PS object and apply type
                $uptime = New-Object -TypeName PSObject -Property $props
                $uptime.PSObject.TypeNames.Insert(0,'BN.Uptime')

                Write-Output $uptime
            }
            catch
            {
                # Check for common DCOM errors and display "friendly" output
                switch ($_)
                {
                    { $_.Exception.ErrorCode -eq 0x800706ba } `
                        { $err = 'Unavailable (Host Offline or Firewall)' }
                    { $_.CategoryInfo.Reason -eq 'UnauthorizedAccessException' } `
                        { $err = 'Access denied (Check User Permissions)' }
                    default { $err = $_.Exception.Message }
                }
                Write-Warning "$computer - $err"
            }
        }
    }

    END {}

}

Usage

  1. Get uptime information for multiple computers.
'dc01','db01','sp01' | Get-Uptime | ft -auto comp*,days,hours,min*,sec*

output:

Computer Days Hours Minutes Seconds
-------- ---- ----- ------- -------
    DC01   17     7       3      58
    DB01   17     4       4      52
    SP01   17     3      55      04
  1. Get uptime information for multiple computers from Active Directory.
ipmo ActiveDirectory
Get-Uptime -cn (Get-ADComputer -f * | select -expand name) | ft -auto comp*,days,hours,min*,sec*

output:

Computer Days Hours Minutes Seconds
-------- ---- ----- ------- -------
    DC01   17     7       3      58
    DB01   17     4       4      52
    SP01   17     3      55      04
  1. Get uptime information for local computer in Unix-like format.
function uptime
{
$time = [System.DateTime]::Now.ToShortTimeString()
$uptime = Get-Uptime
Write-Host $time " up" $uptime.days "day(s)," $uptime.hours "hour(s)," $uptime.minutes "min(s)"
}
uptime

output:

4:53 PM  up 9 day(s), 23 hour(s), 30 min(s)