Please review this reboot script

I use the below script in a powershell module. Use it with invoke-command to run against multiple computers. The problem is when I run from PS v5, it gives a different result as compared to running from PS v3. ( different result means : in PSv3, it gives local time for reboot Time, rather then returning me that Remote computer's Time; while if I run from a machine with PS v5, it gives the
Can someone please explain why
function get-bootTime{
$ht=@{}
$server = $env:Computername
$rawBootTime = get-winevent -FilterHashtable @{logname='system';<wbr />providername='eventlog';id=<wbr />6005} | ? {$_.message -eq "The Event log service was started."} |sort timecreated -desc | select -First 1
$hour1= $rawBootTime.timecreated
$rawBootedON = [datetime]::now - $rawBootTime.timecreated
$uptime = "{0:dd}d {0:hh}h {0:mm}m" -f $rawBootedOn
$ht[$server] = $hour1,$uptime
$ht
}

I guess you could make your life much easier …

Get-CimInstance -ClassName CIM_OperatingSystem -ComputerName “Insert Target ComputerName” | Select-Object -Property LastBootUpTime

Invoke-Command isn’t really needed. Using WMI or even the Get-WinEvent command accept the ComputerName parameter.

function Get-BootTime{
    param (
        [string[]]$ComputerName = $env:Computername
    )
    begin {}
    process{
        $results = foreach ( $computer in $ComputerName ) {
            $rawBootTime = Get-WinEvent -FilterHashtable @{logname='system';providername='eventlog';id=6005} -ComputerName $computer | 
                           Where-Object -FilterScript {$_.message -eq "The Event log service was started."} |
                           Sort-Object -Property timecreated -Descending | 
                           Select-Object -First 1 -ExpandProperty TimeCreated

            $uptime = New-TimeSpan -Start $rawBootTime -End (Get-Date)
 
            [pscustomobject]@{
                Server   = $Computer
                BootTime = $rawBootTime
                UpTime = $uptime
            }
        }
    }
    end {
        $results
    }

}

$bootTime = Get-BootTime -Computer Server1, Server2, Server3
$bootTime

Rather than convert uptime to a string, a TimeSpan is much more useful and it will return in a readable format by default, but if I only care about the hours I don’t have to parse it out.

Get-BootTime -Computer Server1, Server2, Server3


Server                                                               BootTime                                                            UpTime                                                             
------                                                               --------                                                            ------                                                             
Server1                                                          12/6/2018 2:05:13 PM                                                10.18:30:55.0898396                                                
Server2                                                          12/6/2018 2:06:15 PM                                                10.18:29:53.3188560                                                
Server3                                                          12/6/2018 2:10:28 PM                                                10.18:25:40.5488799                                                



PS H:\> $bootTime = Get-BootTime -Computer Server1, Server2, Server3


PS H:\> $bootTime.UpTime


Days              : 10
Hours             : 18
Minutes           : 31
Seconds           : 22
Milliseconds      : 628
Ticks             : 9306826286436
TotalDays         : 10.771789683375
TotalHours        : 258.522952401
TotalMinutes      : 15511.37714406
TotalSeconds      : 930682.6286436
TotalMilliseconds : 930682628.6436

Days              : 10
Hours             : 18
Minutes           : 30
Seconds           : 20
Milliseconds      : 949
Ticks             : 9306209496758
TotalDays         : 10.7710758064329
TotalHours        : 258.505819354389
TotalMinutes      : 15510.3491612633
TotalSeconds      : 930620.9496758
TotalMilliseconds : 930620949.6758

Days              : 10
Hours             : 18
Minutes           : 26
Seconds           : 8
Milliseconds      : 196
Ticks             : 9303681967046
TotalDays         : 10.7681504248218
TotalHours        : 258.435610195722
TotalMinutes      : 15506.1366117433
TotalSeconds      : 930368.1967046
TotalMilliseconds : 930368196.7046


PS H:\> $bootTime.UpTime.Hours
18
18
18