Tail -f in windows

This is a simple PowerShell function that attempts to replicate the Unix command “tail -f”

The only downside is that it outputs from the beginning of the file until it reaches the end, then updates the output as new lines are added to the text file.

<#
.Synopsis
   Tail command for windows
.DESCRIPTION
   replicates the functionality of tail -f
.EXAMPLE
   Usage: tail -filename sometextfile.txt
.INPUTS
   a single text file
.OUTPUTS
   Output to console
.NOTES
   
.COMPONENT
   The component this cmdlet belongs to
.ROLE
   The role this cmdlet belongs to
.FUNCTIONALITY
   Somewhat Replicates the tail -f function of Nix
#>
function tail {
    param (
        $filename
        )
    $filecontent=Get-Content $filename
    $startlength=$filecontent.length
    $filecontent
    while(1){


    $filecontent=Get-Content $filename
    $currentlength=$filecontent.length


    (Get-Content $filename)[$startlength .. $currentlength]


    $startlength=$currentlength
    }
}

Or you could simply do this with:

$filename = "sometext.txt"
Get-Content -Path $filename -Tail 8 -ReadCount 0 -Wait

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.