【PowerShell】tailコマンドのように最終行を読み込む

【PowerShell】tailコマンドのように最終行を読み込む PowerShell

Linuxのtailコマンドのようにテキストファイルの末尾を表示するには
PowerShellのGet-Contentコマンドレットを使用します。
リアルタイムでファイルの最終行に書き込まれたログなどを監視することも
Get-Contentコマンドレットにて-Waitを指定することで実現可能です。

検証したPowerShellバージョン:5.1.22000.832

使い方

書式

Get-Content -Path “ファイルパス” -Wait -Tail 3

-Wait:ファイルを開いたままにし、1秒ごとに出力内容を更新する([Ctrl+C]で終了)
-Tail:ファイルの末尾から何行表示するか行数を指定します

Get-Content -Path “C:\test\ping.txt” -Wait -Tail 3

文字コードの違いでうまく表示ができない場合は末尾に[-Encoding 文字コード]を追記します。
文字コードは以下を指定することができます。

文字コード説明
asciiUses the encoding for the ASCII (7-bit) character set.
bigendianunicodeEncodes in UTF-16 format using the big-endian byte order.
bigendianutf32Encodes in UTF-32 format using the big-endian byte order.
oemUses the default encoding for MS-DOS and console programs.
unicodeEncodes in UTF-16 format using the little-endian byte order.
utf7Encodes in UTF-7 format.
utf8Encodes in UTF-8 format.
utf8BOMEncodes in UTF-8 format with Byte Order Mark (BOM)
utf8NoBOMEncodes in UTF-8 format without Byte Order Mark (BOM)
utf32Encodes in UTF-32 format.
引用元:Get-Content

コメント

タイトルとURLをコピーしました