Find-Uptime :: Powershell script to find uptime of remote computers

System administrators often used to find uptime of computers for various weekly or monthly reports and presentations. There are various commands and third party tools to find uptime of computers.

Here in this article we used PowerShell script to find uptime of bulk remote computers. All you need to have is the hostname or IP address details of the computers saved in the notepad text file.

Powershell script to find uptime of remote computers:

We used Get-Content along with the Read-Host cmdlet to read the inputs from the text file and assign the inputs to variable called .

$Machines.

$machines=get-content((Read-Host 'Enter the computers list file Path here') -replace '"')
1
$machines=get-content((Read-Host 'Enter the computers list file Path here') -replace '"')

Once you have the computer detail all we need to do is to validate whether the device is reachable or not so here we used Test-Connection cmdlet to validate the ping status and assigned the output to the variable $Pingcheck

$Pingcheck= Test-Connection -ComputerName $machine -count 1 -Quiet
1
$Pingcheck= Test-Connection -ComputerName $machine -count 1 -Quiet

In few cases the ping status will return success but the computer will not be reachable, so here we used one more validation to confirm the computer reachability by checking administrative shares of the computers using Test-Patch cmdlet.

$pathtest= Test-Path \\$machine\c$
1
$pathtest= Test-Path \\$machine\c$
We combined both the validation checks to If loop by using AND operator in between both checks as listed below

if($Pingcheck -eq "true" -and $pathtest -eq "true")
{
#Return Code
}
1
2
3
4
if($Pingcheck -eq "true" -and $pathtest -eq "true")
{
#Return Code
}

Once we have all performed all these operation we come to our final step to find uptime, here we used WMI class cmdlets to fetch the uptime of the computers by passing the –ComputerName parameter.

We also used expression ConverttoDateTime to convert the date & time to readable format.

Get-WmiObject win32_operatingsystem -ComputerName $machine| select csname, @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
1
Get-WmiObject win32_operatingsystem -ComputerName $machine| select csname, @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}

Now we have all the details that are required for us to find uptime, all we need to do is combine the cmdlets that we listed into a single script inside simple function like mentioned below.

Function Find-Uptime
{
$machines=get-content((Read-Host 'Enter the computers list file Path here') -replace '"')
foreach($machine in $machines)
{
$Pingcheck= Test-Connection -ComputerName $machine -count 1 -Quiet
$pathtest= Test-Path \\$machine\c$
if($Pingcheck -eq "true" -and $pathtest -eq "true")
{
$uptime=Get-WmiObject win32_operatingsystem -ComputerName $machine| select csname, @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
$uptime
}
else
{
Write-Host "$machine is not reachable" -ForegroundColor Red
}
}
}
Find-Uptime
Function Find-Uptime
{
$machines=get-content((Read-Host 'Enter the computers list file Path here') -replace '"')
foreach($machine in $machines)
{
$Pingcheck= Test-Connection -ComputerName $machine -count 1 -Quiet
$pathtest= Test-Path \\$machine\c$
if($Pingcheck -eq "true" -and $pathtest -eq "true")
{
$uptime=Get-WmiObject win32_operatingsystem -ComputerName $machine| select csname, @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
$uptime
}
else
{
Write-Host "$machine is not reachable" -ForegroundColor Red
}
}
}

Find-Uptime

Copy this script to the notepad file and save it with extension PS1 and execute to find the uptime of the computers that you need.

Once the script is executed the output will be listed as mentioned in the below image.

Hope this article helps, execute this script in test environment before implementing in production. use this script at your own risk as we do not provide any guarantee or warranty for any loss occurred.

Leave a Comment