Refactored Codes
Find Os Last Boot Time and Os Up Time in powershell
This information is provided by Get-ComputerInfo command of the powershell. We can follow below steps to get the information. Method-1  : Find Os Up Time
Get-ComputerInfo -Property OsUpTime
Find Os Last Boot Up Time
Get-ComputerInfo -Property OsLastBootUpTime
But, the above methods are inefficient, because Get-ComputerInfo takes sometime to run. Method 2 : We first store the output of Get-ComputerInfo in a variable : $computerInfo. The, we can extract other information from $computerInfo object.
$computerInfo = Get-ComputerInfo;
Find the Os Up Time
$computerInfo.OsUptime
Find the Os Last Boot Up time
$computerInfo.OsLastBootUpTime
We can extract other information from the $computerInfo object. This way of scripting is more efficient.