EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Tail
 

PowerShell Tail

Updated March 6, 2023

PowerShell Tail

 

 

Definition of PowerShell Tail

PowerShell tail command which is a Unix equivalent Tail command and considered as a parameter in the PowerShell’s Get-Content cmdlet and it is to retrieve the specified number of lines from the bottom of the content of the file and it can wait for the next continuous logs and with -Wait parameter it can wait until the new logs are retrieved and display it on the console or can be saved to another log file using the pipeline.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

Get-Content Syntax:

Get-Content
[-ReadCount <Int64>] [-TotalCount <Int64>] [-Tail <Int32>] [-Path] <String[]>
[-Filter <String>] [-Include <String[]>] [-Exclude <String[]>] [-Force] [-Credential <PSCredential>] [-UseTransaction] [-Delimiter <String>] [-Wait] [-Raw] [-Encoding <FileSystemCmdletProviderEncoding>] [-Stream <String>] [<CommonParameters>]

Get-Content
[-ReadCount <Int64>] [-TotalCount <Int64>] [-Tail <Int32>] -LiteralPath <String[]>
[-Filter <String>] [-Include <String[]>] [-Exclude <String[]>] [-Force] [-Credential <PSCredential>] [-UseTransaction] [-Delimiter <String>] [-Wait] [-Raw] [-Encoding <FileSystemCmdletProviderEncoding>] [-Stream <String>] [<CommonParameters>]

Although for the PowerShell tail command, we aren’t going to use the whole set we need the specific parameters like -Wait, -Tail, etc. We can also minimize this command as shown below.

Get-Content -Path Filepath -Tail <int>

Or,

Get-Content -Path Filepath -Tail <int> -Wait

PowerShell Tail equivalent command – Select-Object -Last <int>

Syntax:

Get-Content -Path Filepath | Select-Object -Last <int>

Description:

The Tail command is popular in the Unix language and it is used to retrieve the specific number of lines from the end of the document or the log files. PowerShell doesn’t have the command with the same name but from the PowerShell v3.0 onwards, PowerShell has added -Tail parameter in the Get-Content cmdlet.

The get-Content command is useful in retrieving the content of a file whether it is a script, log file, text file, and with the -Tail parameter we can retrieve the number of lines from the end of the file. For example,
We want to retrieve the last 2 lines of the Servers.txt file. First, we will check the content of the Servers.txt for our better understanding.

Get-Content C:\temp\Servers.txt

Output:

Powershell tail 1

Get-Content C:\temp\Servers.txt -Tail 2

Output:

Powershell tail 2

If your file is the log file that continuously gets updated like the monitoring logs, database logs, mailbox logs, etc in that case we can use the -Wait parameter which waits for the new input to the specified file and displays on the screen. We will see this in the Examples section.

Examples

Let us discuss examples of PowerShell Tail.

Example #1: Retrieving the last 5 lines from the text file.

In this example, we have the list of azure locations stored in the text file Locations.txt and we need the last 5 locations from the file. So we will use the below command.

Get-Content C:\Temp\locations.txt -Tail 5

Output:

Powershell tail 3

Example #2: Using the Get-Content aliases to read the file and retrieve the last 5 lines.

We know that PowerShell can run the few Unix and DOS commands in the PS console because these commands are used as PowerShell aliases. For example,

Get-Alias cat

get alias

So we can run the Unix cat command instead of the Get-Content and it will support all the parameters that the Get-Content command supports.

cat C:\temp\locations.txt -Tail 5

Output:

Powershell tail 4

In fact, we can use any aliases that are supported by the Get-Content command and can select few lines with the -Tail parameter. Below are the supported aliases.

Get-Alias -Definition Get-Content

Output:

Get alias 1

Here, we can use cat, gc, or type instead of the Get-Content command. We can write the above example as,

type C:\temp\locations.txt -Tail 5

or,

gc C:\temp\locations.txt -Tail 5

Example #3: Using the -Wait and -Tail combined parameters with the Get-Content command.

-Wait parameter keeps the file opens in the PowerShell console until the console is interrupted by the Ctrl+C command. It checks if the file has any new updates every one second and if the file has the new input, it displays it on the console.

In this example, we have a text file called processes.txt which has a list of processes and it updates whenever any new process starts with the last 4 processes displayed on the screen.

Get-Content C:\Temp\processes.txt -Tail 4 -wait

Output:

Output

Now we when the file is updated by the new processes then they should display the output on the console.

Get content

Press Control+C when you want to terminate the above command output.

Example #4: Using Select-Object command with the -Last parameter.

There is another alternative option of the -Tail parameter in the Get-Content command is to use the Select-Object in the Pipeline with the -Last parameter which works similar to the Tail command.

Get-Content C:\Temp\locations.txt | Select-Object -Last 5

Output:

Select object

Please note: You can use the Select alias instead of the Select-Object.

The Select-Object has also the -First parameter which retrieves the first specified lines but this won’t serve the purpose of the Tail command.

The Select-Object command is helpful with the -Last parameter more than the -Tail parameter in the Get-Content command because the Get-Content command is only used with the files but if we want to retrieve the last few lines directly from the command then the Tail command will not be helpful but Select-Object will be used there.

For example, we need to retrieve the list of the last 5 services then we can use the Select-Object command as shown below.

Get-Service | Select -Last 5

Output:

output 1

Example #5: Using Filter in Get-Content command.

We can use the Where-Object command to filter the specific content. For example, we need in the locations.txt file that if the “East Us” region is entered then it should display the output. The code is shown below.

cat C:\Temp\locations.txt -Tail 1 -Wait | where{$_ -match "East US"}

Output:

output 2

Once the East US is entered into the locations.txt file,

-wait

Conclusion

PowerShell tail command is very helpful when we are dealing with the new content of the file because many times we need to see updated logs on the screen and redirect the new logs to another storage location or we need to take action based on the recently updated file. Logs can be monitoring logs, security logs, database logs, etc.

Recommended Articles

This is a guide to PowerShell Tail. Here we discuss definition, syntax, and parameters, examples with code implementation. You may also have a look at the following articles to learn more –

  1. PowerShell ZIP
  2. PowerShell Multiline String
  3. PowerShell Batch File
  4. PowerShell do while
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW