PowerShell : Uptime scripts

I wanted a quick way to determine uptime for my servers and other windows computers and so started searching for solutions. After some time I finally found some good information but it wasn’t entirely user friendly. After another 2 hours of researching and trying to get it right, I came up with these scripts for displaying your computer’s uptime. The PowerShell scripts aren’t mine (you’ll find links to the originals below) but the batch files are. Pretty simple but a nice time saver.

Simple Uptime:
batch file: uptime.bat

@echo off
powershell.exe -noexit -ExecutionPolicy Bypass -file .\uptime.ps1 \r

PowerShell Script: uptime.ps1

<# .SYNOPSIS Demonstrates uptime using WMI .DESCRIPTION This script used Win32_ComputerSystem to determine how long your system has been running. This is a rewrite/improvement of sample 3 at http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx. .NOTES File Name : Get-UpTime.ps1 Author : Thomas Lee - [email protected] Requires : PowerShell V2 CTP3 .LINK Script Posted to: http://www.pshscripts.blogspot.com Original sample posted at: http://msdn.microsoft.com/en-us/library/aa394591(VS.85).aspx .EXAMPLE PS c:foo> .Get-UpTime.Ps1 
    System Up for: 1 days, 8 hours, 40.781 minutes 
#  Start of script
# Helper Function - convert WMI date to TimeDate object
function WMIDateStringToDate($Bootup) {
# Main script
$Computer = "." # adjust as needed
$computers = Get-WMIObject -class Win32_OperatingSystem -computer $computer
foreach ($system in $computers) {
    $Bootup = $system.LastBootUpTime
    $LastBootUpTime = WMIDateStringToDate($Bootup)
    $now = Get-Date
    $Uptime = $now - $lastBootUpTime
    $d = $Uptime.Days
    $h = $Uptime.Hours
    $m = $uptime.Minutes
    $ms= $uptime.Milliseconds
    "System Up for: {0} days, {1} hours, {2}.{3} minutes" -f $d,$h,$m,$ms
# End script

PowerShell Source: Link

Formated Uptime:
batch file: uptimef.bat

@echo off
powershell.exe -noexit -ExecutionPolicy Bypass -file .uptimef.ps1 r

PowerShell script: uptimef.ps1

# This script permits to get UpTime from localHost or a set of remote Computer
# usage
# localHost
# .BootTime.ps1
# set of remote computers
# get-content .MyserverList.txt | .boottime.ps1
# Optionally pipe output to Export-Csv, ConverTo-Html
Process {
    $ServerName = $_
    if ($serverName -eq $Null) {
        $serverName= $env:COMPUTERNAME
    $timeVal = (Get-WmiObject -ComputerName $ServerName -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem").LastBootUpTime
    $DbPoint = [char]58
    $Years = $timeVal.substring(0,4)
    $Months = $timeVal.substring(4,2)
    $Days = $timeVal.substring(6,2)
    $Hours = $timeVal.substring(8,2)
    $Mins = $timeVal.substring(10,2)
    $Secondes = $timeVal.substring(12,2)
    $dayDiff = New-TimeSpan  $(Get-Date –month $Months -day $Days -year $Years -hour $Hours -minute $Mins -Second $Secondes) $(Get-Date)
    $Info = "" | select ServerName, Uptime
    $Info.servername = $servername
    $d =$dayDiff.days
    $h =$dayDiff.hours
    $m =$dayDiff.Minutes
    $s = $daydiff.Seconds
    $info.Uptime = "$d Days $h Hours $m Min $s Sec"


You can click on the links above to download. These scripts will allow you to quickly see the uptime on your PC. I’ll be posting more scripts as time goes on. Feel Free to suggest something if you’re interested.

Verified by MonsterInsights