Powershell

How to use PowerShell Tail with Get-Content

The Get-Content cmdlet of PowerShell allows you to get the file’s content on which it is applied. The Get-Content cmdlet of PowerShell serves a long list of actions by the parameters supported by it. The Tail is one of the parameters that prints the data from the bottom of the file. The number of lines(to be printed) can also be specified by using the -Tail command. This article demonstrates the working mechanism and usage of Tail with the Get-Content command.

How the Tail parameter works with Get-Content

The Tail parameter works with several cmdlets and functions of PowerShell. This section will provide a basic working of Tail with the Get-Content cmdlet. As discussed earlier, the Get-Content command is used to get the content of an item and print it on the screen. When the Tail parameter is applied with Get-Content, the syntax would be like as shown below:

> Get-Content -Path <Path-of-the-file> -Tail <integer>

In the above-stated syntax:

  • The <Path-of-the-file> represents the exact path of the file on which you want to apply the Get-Content cmdlet.
  • The <integer> defines the lines count that would be printed using the Tail parameter. This option of the Tail command is mandatory to be used (you can set it to 0 or the maximum ); otherwise, you won’t be able to execute the Tail parameter.

How to use Tail with Get-Content

In this section, the Tail parameter is practiced with the Get-Content cmdlet. To better understand, we are starting this section to demonstrate the difference between the Get-Content cmdlet and Tail with Get-Content cmdlet.

We have a demo text file named linuxhint.txt, and the command written below retrieves the insides of that file:

> Get-Content -Path E:\linuxhint.txt

Now using the same command with the -Tail parameter will return only the last line (as we have passed 1 to Tail) of the linuxhint.txt file:

> Get-Content -Path E:\linuxhint.txt -Tail 1

However, the number passed to the -Tail parameter can be increased to the maximum number of lines available in that text file. For instance, the commands written below practice the Tail number with different numbers.

The below-mentioned command applies Tail with Get-Content using the “3” number that shows the last three lines will be printed:

> Get-Content -Path E:\linuxhint.txt -Tail 3

Moreover, you can get no output if the Tail option is used with “0” as can be seen in the following command:

> Get-Content -Path E:\linuxhint.txt -Tail 0

Here a question arises, what happens if an invalid number is inserted with the Tail parameter?

Let’s practice this on the same text file (linuxhint.txt). Keep in mind that the linuxhint.txt file contains only five(5) lines.

We have executed Tail with Get-Content and 25 is passed as a Tail number in that command. Although there are only five lines, exceeding the maximum number will print all the content of the linuxhint.txt file.

> Get-Content -Path E:\linuxhint.txt -Tail 25

Note: Invalid numbers may exceed the maximum number of lines or may decrease the minimum. For instance, in the case of the linuxhint.txt file, numbers greater than 5 or less than 0 are referred to as invalid numbers.

Conclusion

The Get-Content cmdlet of PowerShell permits acquiring the content of a file. The Tail parameter is used with the Get-Content cmdlet to retrieve the specific number of lines from the bottom of a file. This article enlightens the basic working and usage of Tail with the Get-Content cmdlet. We have provided a detailed section that explains the idea of the Tail parameter with Get-Content in a better way. It is observed that if you cross the upper or lower limit (in the context of number of lines), then Tail with Get-Content prints all the content inside that file.

About the author

Adnan Shabbir