PowerShell Tail Command

Unix has a tail command which helps us in reading the file content in the shell window. If the file size is enormous, we could run a simple tail command and read the last 100 lines of the file. We use this in reading the vast logs, which will take more time to open or download. So What is the Unix tail equivalent command in Windows Powershell?

PowerShell version 1.0 and 2.0 don’t have the built-in tail command. However, there is an alternate approach to reading the file contents. Let’s take a look at it in detail.

Unix tail equivalent command in PowerShell Version 1.0 and 2.0

Running the below command will show the lines that get added to the file. To see the live example run this command and try to update the text file and come back to the console, and you can see the updated text displayed here.

Get-Content -Path "C:\scripts\test.txt" -Wait

PowerShell Tail Command Version 3.0 and above

PowerShell 3.0 and above comes up with built-in flag -Tail on Get-Content. The example below demonstrates the -Tail flag.

Get-Content ./log.log -Tail 10

gets the last 10 lines of the file

Get-Content ./log.log -Wait -Tail 10

gets the last 10 lines of the file and waits for more

Note: Get-Content also has aliases defined. For example, if you are used to UNIX you might like cat, and there are also type and gc.

# Print whole file and wait for appended lines and print them
cat <Path> -Wait
# Print last 10 lines and wait for appended lines and print them
cat <Path> -Tail 10 -Wait

0 Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
PowerShell

PowerShell Concatenate String

Table of Contents Hide Introduction to PowerShell Concatenate StringHow to Concatenate Strings and Variables in PowerShell#1. Expanding Strings#2. Substitution…