このエントリーをはてなブックマークに追加


Windowsの起動時刻の取得と稼働時間の取得方法

PowerShellを使って、Windowsの起動時刻と稼働時間を取得する方法を以下に記します。
Linuxなどでは、uptimeコマンドの稼働時間取得と似ています。


関連記事

起動時刻の取得

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

注意
このLastBootUpTimeの値は、systeminfoコマンドで取得できる「システム起動時間」と同じある。
Windows10で上記PowerShellコマンドおよびsysteminfoコマンドを実行したが、高速スタートアップの場合、
シャットダウンしても起動したままになっている模様である。
再起動すれば、起動時間は変更(リセット)されているのが確認できました。

> 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などであれば、タスクマネージャの画面で確認することができます。

01.png

以上、WindowsのPowerShellを使って、起動時間と稼働時間を取得し表示する方法でした。



添付ファイル: file01.png 992件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2016-01-16 (土) 22:30:01