A command like Linux `tail` in PowerShell?

7,266

Solution 1

As of PowerShell 3 the Get-Content (alias gc) cmdlet supports -Tail and -Wait parameters when used on a filesystem. Look it up with help gc.

Solution 2

The native PS equivalent since PSv3 is

Get-Content -Last n

which is also fast. In PSv2 and below you have to make do with

Get-Content filename | Select -Last n

but that has several caveats. It cannot block and wait for new changes to the file for example and is also not very efficient in that it has to read the file from the start completely before being able to show the last lines.

PSCX has a Get-FileTail command which has a -Wait parameter:

Name

Get-FileTail

Synopsis

PSCX Cmdlet: Tails the contents of a file - optionally waiting on new content.

Syntax

Get-FileTail [-Path] <String[]> [-Count <Int32>] [-Encoding <EncodingParameter>] [-LineTerminator <String>]
[-Wait] [<CommonParameters>]

Get-FileTail [-LiteralPath] <String[]> [-Count <Int32>] [-Encoding <EncodingParameter>] [-LineTerminator <String>]
[-Wait] [<CommonParameters>]

Description

This implentation efficiently tails the cotents of a file by reading lines from the end rather then processing the entire file. This behavior is crucial for efficiently tailing large log files and large log files over a network. You can also specify the Wait parameter to have the cmdlet wait and display new content as it is written to the file. Use Ctrl+C to break out of the wait loop. Note that if an encoding is not specified, the cmdlet will attempt to auto-detect the encoding by reading the first character from the file. If no character haven't been written to the file yet, the cmdlet will default to using Unicode encoding. You can override this behavior by explicitly specifying the encoding via the Encoding parameter.

Get-FileTail is aliased to tail by default if you install PSCX.

Share:
7,266

Related videos on Youtube

jsalonen
Author by

jsalonen

Updated on September 18, 2022

Comments

  • jsalonen
    jsalonen over 1 year

    How can I replicate the behaviour of Linux's tail in PowerShell?

    I'm running an application that writes a log file (error.log) and I would like to see the last lines from it as well as keep the console updating changes.

    So is there an equivalent of something like tail -f filename in PowerShell?

  • jsalonen
    jsalonen over 11 years
    Installed PSCX 3.0 and tail works just as it should now. This is great, thanks!
  • Snesticle
    Snesticle over 11 years
    Thought you guys were talking about PCSX which is way cooler than PSCX.
  • Joey
    Joey over 11 years
    Huh? Why would we talk about a Playstation emulator? And I take PowerShell over that any day, thanks.
  • kmote
    kmote about 11 years
    Just an update: I installed PS 3.0 (without PSCX) and found Get-Content... -last n to be extremely efficient on a 1.7GB file. I was able to read the tail with no perceptible delay on a rather slow machine.
  • Joey
    Joey about 11 years
    @kmote, yes, PowerShell v3 has this built-in but at the time of writing that answer I didn't yet use Windows 8 (and thus PS v3) I think.