EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Tail
Secondary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell Tail

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.

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>]

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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>

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,294 ratings)

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
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more