There was a question on StackExchange/SuperUser, How to know how long I have launched this machine, asking the equivalent of uptime in Linux. Please see my answer below using PowerShell.
Get-CimInstance -ClassName win32_operatingsystem | Select-Object csname, @{name="Uptime"; expression = {((Get-Date)-($_.lastbootuptime))}}
csname Uptime
------ ------
ca1 12.23:50:46.6914567
New-TimeSpan -start (Get-CimInstance -ClassName win32_operatingsystem).LastBootUpTime -end (Get-Date)
Days : 12
Hours : 23
Minutes : 52
Seconds : 35
Milliseconds : 887
Ticks : 11227558871585
TotalDays : 12.9948598050752
TotalHours : 311.876635321806
TotalMinutes : 18712.5981193083
TotalSeconds : 1122755.8871585
TotalMilliseconds : 1122755887.1585
New-TimeSpan -start (Get-CimInstance -ClassName win32_operatingsystem).LastBootUpTime -end (Get-Date) | Select-Object -Property Days,Hours,Minutes
Days Hours Minutes
---- ----- -------
12 23 53
The above code blocks display multiple ways of retrieving the uptime of a machine. Using Get-CimInstance and querying the win32_operatingsystem WMI class for the LastBootUpTime value enables a simple calculation of the time right now Get-Date minus LastBootUpTime. The calculation is being performed in a Select-Object calculated propery. The first example could be added into a ForEach Statement to iterate through multiple machines. The name of the machine and uptime are displayed together.
New-TimeSpan creates a TimeSpan object can be used to add or subtract time from DateTime objects. Using New-TimeSPan with a -Start and -End Parameter, the LastBootUpTime and current time Get-Date are compared and the difference returned.
In the final code block, Select-Object is being used to retrieve only the desired properties.
Gets the CIM instances of a class from a CIM server.
Specifies the name of the CIM class for which to retrieve the CIM instances. NOTE: You can use tab completion to browse the list of classes, because PowerShell gets a list of classes from the local WMI server to provide a list of class names.
Selects specific properties of an object or set of objects.
Specifies the properties to select.
The New-TimeSpan cmdlet creates a TimeSpan object that represents a time interval. You can use a TimeSpan object to add or subtract time from DateTime objects.
Specified the start of a time span. Enter a string that represents the date and time or a DateTime object.
Specifies the end of a time span.
Being able to run a ‘one liner’ to determine the uptime a machine saves a lot of time. You don’t have to login to the machine and check event logs or the Ethernet Status. You could also run it remotely if you have PowerShell Remoting setup.
Hope you’re having a great day and this is of use.
Thanks, Tim.