Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
See also: stackoverflow.com/questions/4426442/…, and Get-Content -Tail –  Jay Bazuzi Nov 8 '12 at 15:46
add comment

2 Answers

up vote 6 down vote accepted

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.

share|improve this answer
add comment

The native PS equivalent is

Get-Content filename | Select -Last n

but with 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|improve this answer
1  
Installed PSCX 3.0 and tail works just as it should now. This is great, thanks! –  jsalonen Nov 8 '12 at 11:44
1  
Thought you guys were talking about PCSX which is way cooler than PSCX. –  Snesticle Nov 8 '12 at 12:28
2  
Huh? Why would we talk about a Playstation emulator? And I take PowerShell over that any day, thanks. –  Јοеу Nov 8 '12 at 13:18
    
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. –  kmote Mar 22 '13 at 14:43
    
@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. –  Јοеу Mar 22 '13 at 19:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.