I know when you last rebooted. Uptime Scripts!
My end users are resistant to reboots, and I’m too cozy in my office to manually check uptime. That’s where this handy utility comes in!
TL;DR >>>> GitHub for singles & GitHub for bulk
Section 1 | Target Acquired
Let’s first see how we can target an individual computer’s uptime by capturing WMI information. This is short and sweet. We’ll first create a While loop and set it to always be $true. Then we’ll prompt the you (or the admin user) to punch in the PC name. Next, capture WMI info with a “Get-WMIObject” command leveraging the win32_operatingsystem class. The rest of the script is just for interacting with the script in a pseudo UI.
while ($true){
$PCName = Read-Host -Prompt "Enter hostname of PC to find uptime"
$Uptime = Get-WmiObject win32_operatingsystem -ComputerName $PCName | select csname, @{LABEL=’LastBootUpTime’ ;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
Write-host "`n $Uptime `n"
pause
cls
}
It will return like this:

Short. Simple. Sweet!
Section 2 | More Than a Handful
Now that we have a good basis, let’s hit this command against a collection of PCs. This is good for tackling many PCs that may be afflicted with a similar diagnosis or maybe seeing how many PCs haven’t been rebooted to apply their monthly updates yet….
First, prep a .txt file containing the hostnames of the PCs you are targeting. Identify the path to your file within this script on line 1 (or line 12 if you’ve pulled it from GitHub). We are then going to run through a foreach loop, first pinging the PC to see if its even online (we don’t want to waste compute time on offline PCs), then we’ll run the same Get-WMIObject cmdlet from section 1, and output the results to a .csv file. For PCs that weren’t reachable, we’ll create a separate txt file containing those entries.
foreach($PC in (Get-Content -Path "PATH\FILE.txt")){
if ( test-connection $PC -count 1 -quiet ){
$Uptime = Get-WmiObject win32_operatingsystem -ComputerName $PC | select csname, @{LABEL=’LastBootUpTime’ ;EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
Write-host "`n $Uptime `n"
$Uptime | Export-Csv -Path "c:\temp\Uptime\UptimeOut.txt" -Append -NoClobber -NoTypeInformation
} Else {
$status = " is unreachable"
$PC + $status | Export-Csv -Path "c:\temp\Uptime\UptimeOut_Offline.txt" -Append -NoClobber -NoTypeInformation
}
}
All you need to do is compile a list of PCs and run it!
Closing Time
A good admin knows when and how to not leave the comfort of their dwelling just for a reboot. Use this script to do just that!
Remember:
This project is provided “as is” without any warranty of any kind, express or implied. Use it at your own risk. The authors and contributors are not responsible for any damage, data loss, or other issues that may arise from using this software. You are solely responsible for any actions taken based on this code.