Windowsの起動時刻の取得と稼働時間の取得方法 †PowerShellを使って、Windowsの起動時刻と稼働時間を取得する方法を以下に記します。 関連記事 †起動時刻の取得 †PowerShellを起動して、以下のコマンドを実行するとWindowsを起動した時刻が表示されます。 (Get-WmiObject Win32_OperatingSystem).LastBootUpTime 実行例 PS C:\> (Get-WmiObject Win32_OperatingSystem).LastBootUpTime 20160101214117.495750+540 これでは並んでわかりづらいので変換して表示してみます。 [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime) 実行例 PS C:\> [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime) 2016年1月1日 21:41:17 尚、取得した起動時間のオブジェクトは以下のようにDateTimeになっているのが確認できます。 PS C:\Users\sakura> $boottime=( [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime)) PS C:\Users\sakura> $boottime.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True DateTime System.ValueType PS C:\Users\sakura> "$($boottime.Year)/$($boottime.Month)/$($boottime.Day) $($boottime.Hour):$($boottime.Minute):$($boottime.Second)" 2016/1/4 22:0:8 注意 > systeminfo <snip> システム起動時間: 2016/01/01, 21:41:17 <snip> 稼働時間を取得する †Linuxのuptimeコマンドが表示する、稼働時間をPowerShellを使って表示させてみます。 現在時刻から上記の起動時間を引き算して求める。 [DateTime]::Now - [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime) または (Get-Date) - [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime) 実行例 PS C:\Users\sakura> [DateTime]::Now - [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime) Days : 0 Hours : 0 Minutes : 10 Seconds : 5 Milliseconds : 758 Ticks : 6057585232 TotalDays : 0.00701109401851852 TotalHours : 0.168266256444444 TotalMinutes : 10.0959753866667 TotalSeconds : 605.7585232 TotalMilliseconds : 605758.5232 これでは見にくいので、以下のようにすればよいでしょう。 PS C:\Users\sakura> (Get-Date) - [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime) | Format-Table Days Hours Minutes Seconds Milliseconds ---- ----- ------- ------- ------------ 0 0 15 29 152 または PS C:\Users\sakura> $boottime=( [Management.ManagementDateTimeConverter]::ToDateTime((Get-WmiObject Win32_OperatingSystem).LastBootUpTime)) PS C:\Users\sakura> (Get-Date) - $boottime | Select-Object Days, Hours, Minutes, Seconds Days Hours Minutes Seconds ---- ----- ------- ------- 0 0 16 36 GUIにて確認する †Windows8.1やWindows10などであれば、タスクマネージャの画面で確認することができます。 以上、WindowsのPowerShellを使って、起動時間と稼働時間を取得し表示する方法でした。 |