Restart host after certain days of uptime

3. December 2015

PowerShell Version: >1
Modules: none

Nobody wants Windows hosts with too much uptime so this script combined with a planned task during the weekly maintenance downtime might help:

# prepare WMI class
$gwmi= Get-WmiObject -Class Win32_OperatingSystem -Computer localhost
# convert LastBootUpTime and select only the "Days" value
$uptime = [DateTime]::Now - $gwmi.ConvertToDateTime($gwmi.LastBootUpTime) | select Days
# check if uptime is gt 60 days and if so -> restart
if ($uptime.days -gt 60)

You might want to use the -force parameter on Restart-Computer for an immediate restart.


Leave a Reply

Your email address will not be published. Required fields are marked *