Implementing the Unix “tail” command with PowerShell

Posted: (EET/GMT+2)

 

The Unix and Linux operating systems natively support a command called "tail", which allows you to view the last lines of a text files, such as a log file very conveniently.

On the Windows operating system, there isn't any built-in command for this in the traditional command shell. However, PowerShell does contain what you need, even though the command name is different.

To implement "tail" in PowerShell 3.0 and later (aka Windows 8 or Windows Server 2012 or later), you can use the Get-Content cmdlet with the "-Tail" parameter. For example:

Get-Content C:\Logs\MyLogFile.txt -Tail 5

This would display the last five lines of the specified file. Since the Get-Content cmdlet name is somewhat long, you can use an convenient alias for it. In fact, there are three such aliases: "gc", "type" and "cat".

Additionally, you can even add the "-Wait" parameter, which starts to listen changes to the log file, and output all new lines written to the file into the console window.

Very nice!