Get Network Computers Uptime using Powershell

What is your Network Computers Uptime?The problem

Computers just need to be rebooted.  Its a simple fact.  If you manage a lot of computers, keeping track of the last time a reboot happened may be an issue.  Here’s a down and dirty script to get all of your active directory network computers uptime.  I’ve even given an option to tell you when a system is overdue for a reboot based on you own definition of how log is $toolong.

The Code – Network Computers Uptime
# NETWORK COMPUTERS UPTIME - FJM 
#Gimme a clean screan
clear
# Here is what to do if you encounter an error
$ErrorActionPreference = "silentlycontinue"

# Get a list of comuters from Active Directory
Import-Module ActiveDirectory
$computer = (Get-ADComputer -Filter { (ObjectClass -eq "Computer") -and (Enabled -eq "True") } | Select -Expand DNSHostName)

# Set number of days system has been up to flag.
$toolong=28

# Get the work done
Write-Host "Computers that have been up for over $toolong days will be flagged in " -NoNewline
Write-Host "magenta" -ForegroundColor Magenta
foreach ($computer1 in $computer) {

<# This command is the asy method of just getting your information, but I wanted to present it
in a way that was easier to read and flagged computers that have been up too long.

Get-Uptime -ComputerName $computer1

#>

<# This code does the same thing as Get-Uptime -ComputerName $computer1 only fancier
 #>
$uptime=(get-date) – (gcim Win32_OperatingSystem -ComputerName $computer1).LastBootUpTime
"---------------------------------------------------"
If ($uptime.Days -gt $toolong) {
Write-Host $computer1 -foregroundcolor Magenta -NoNewline
Write-Host " Overdue for Reboot" -ForeGroundColor Cyan
Write-host "UpTime: " -foregroundcolor Yellow -NoNewline
Write-host $uptime.Days "Days " -NoNewline
Write-host $uptime.Hours "Hours " -NoNewline
Write-host $uptime.Minutes "Minutes " 
}
else{
Write-Host $computer1 -foregroundcolor Green
Write-host "UpTime: " -foregroundcolor Yellow -NoNewline
Write-host $uptime.Days "Days " -NoNewline
Write-host $uptime.Hours "Hours " -NoNewline
Write-host $uptime.Minutes "Minutes "
}
}

#< Like this?  Checkout more at http://www.xpertnotes.net #>

Now that you know your network computers uptime, shouldn’t you be rebooting them?