powershelltimereturn-valueselection

Get UpTime from powershell into a usable way, but can't get it to work


I've been making this program where i need to send a command to powershell and in return it gives me the sys UpTime (minutes work better but not mandatory)

As i'm still not used to using powershell, i'm having a lot of problems in getting this intel.

This is what i tryed:

    (get-date) - (gcim Win32_OperatingSystem).LastBootUpTime

Gives me the uptime, but i still have no idea how to work with that, so i still need to somehow add something like:

    | Select-String -Pattern "TotalMinutes"

But then i need (somehow) to make that powershell gives me that time as return so i can work with it. maybe to clipboard?

    | clip

But if i add all those up, none will work. Putting in the clipboard is just a way i made to get this info, others might also work. I'm still very new to this, sorry if i hurt your intellect with stupid questions.

Thanks in advance


Solution

  • By subtracting two [datetime] (System.DateTime) instances, you get a [timespan] (System.TimeSpan) instance, which you can store in a variable:

    $timeSpanSinceBoot = (Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
    

    You can then access its properties as needed, such as .TotalMinutes:

    $timeSpanSinceBoot.TotalMinutes
    

    To examine the members of the time-span value's type, use the Get-Member cmdlet:

    $timeSpanSinceBoot | Get-Member # lists properties and methods