PowerShell Tail Equivalent

Saw this tweet yesterday about an equivalent of tail -f in PowerShell

(I think Simon meant Get-Content)

In PowerShell v3 the Get-Content cmdlet received an additional parameter -Tail. This means rather than pulling back the whole of a text or log file, you can just get the last x lines, e.g.


Get-Content .\\vmkernel.log -Tail 10

Nice, but how can we make this more friendly to those used to typing tail -f?

PowerShell ships with some built-in aliases to help shorten your typing when working at the command line and / or to decrease the initial learning curve. For example cat is an alias for Get-Content, so we could type:


cat .\\vmkernel.log -Tail 10

but that just looks a bit weird!

So, since it is possible to create your own aliases with New-Alias, we can combine that with the below Get-ContentTail function to get an experience similar to tail. Stick both in your PowerShell profile and it’ll be available anytime you open a session.


New-Alias tail Get-ContentTail

As Simon also points out above, we can use the -Wait parameter to get a similar experience to -f in Unix. So with my modified version you can do this:


tail .\\vmkernel.log 10 -f

Now if I simulate (via Wordpad) an application adding lines to that file

you’ll see the lines appear in the PowerShell session.


function Get-ContentTail { <# .SYNOPSIS Get the last x lines of a text file

.DESCRIPTION Get the last x lines of a text file

.PARAMETER Path Path to the text file

.PARAMETER Lines Number of lines to retrieve

.INPUTS IO.FileInfo System.Int

.OUTPUTS System.String

.EXAMPLE PS> Get-ContentTail -Path c:\\server.log -Lines 10

.EXAMPLE PS> Get-ContentTail -Path c:\\server.log -Lines 10 -Follow

#> \[CmdletBinding()\]\[OutputType('System.String')\]

Param (

\[parameter(Mandatory=$true,Position=0)\] \[ValidateNotNullOrEmpty()\] \[IO.FileInfo\]$Path,

\[parameter(Mandatory=$true,Position=1)\] \[ValidateNotNullOrEmpty()\] \[Int\]$Lines,

\[parameter(Mandatory=$false,Position=2)\] \[Switch\]$Follow ) try {

if ($PSBoundParameters.ContainsKey('Follow')){

Get-Content -Path $Path -Tail $Lines -Wait

} else {

Get-Content -Path $Path -Tail $Lines }

} catch \[Exception\]{

throw "Unable to get the last x lines of a text file....." } }