This is a script that I use alot. It uses WMI to query a remote computer and return an object showing how long the computer have been running since last reboot.
#Requires -Version 2 [CmdletBinding()] param( [Parameter(ValueFromPipeline = $true, ValueFromPipelinebyPropertyName = $true)] [String[]]$ComputerName = ([System.Net.Dns]::GetHostName()), [string]$logfile = 'uptime-failed.txt' ) BEGIN{Remove-Item $logfile -ErrorAction SilentlyContinue} PROCESS { foreach ($computer in $ComputerName){ $continue = $true try {$wmi = Get-WmiObject -ComputerName $computer -ErrorAction Stop -Query "SELECT LastBootupTime FROM Win32_OperatingSystem"} catch { $continue = $false [string]$wmiException = $_.Exception.Message Write-Verbose $computer" - Query failed" if ($wmiException){Write-Debug $computer" - $wmiException"} $computer | Out-File $logfile } if ($continue){ $lastBootup = $wmi.ConvertToDateTime($wmi.LastBootupTime) [TimeSpan]$uptime=New-TimeSpan $lastBootup $(Get-Date) $obj = New-Object -TypeName PSObject $obj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $computer -PassThru | Add-Member -MemberType NoteProperty -Name Days -Value $($uptime.days) -PassThru | Add-Member -MemberType NoteProperty -Name Hours -Value $($uptime.hours) -PassThru | Add-Member -MemberType NoteProperty -Name Minutes -Value $($uptime.minutes) -PassThru | Add-Member -MemberType NoteProperty -Name Seconds -Value $($uptime.seconds) Write-Output $obj}}}