6

Windows: PowerShell `Tail`, & `Head` Equivalents

 8 months ago
source link: https://www.shellhacks.com/windows-powershell-tail-head-equivalents/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Windows: PowerShell `Tail`, & `Head` Equivalents

The tail and head are two of the most commonly used commands for displaying the contents of a file in Linux.

Windows PowerShell, in its turn, also has equivalents of the Linux tail and head commands.

From this short post you will learn how to use the equivalents of the tail and head commands in Windows PowerShell and how to dynamically print a file’s contents, i.e. follow it like using tail -f.

Cool Tip: Windows watch command equivalent in CMD and PowerShell! Read more →

Tail and Head Commands in Windows PowerShell

PowerShell Tail Command

To tail the output of a PowerShell command in Windows, execute:

PS C:\> <command>| Select -Last <numberOfLines>
- example -
PS C:\> systeminfo | Select -Last 10

To print the last few number of lines of a file in Windows, use a combination of the PowerShell Get-Content command with the -Tail option:

PS C:\> Get-Content <fileName> -Tail <numberOfLines>
- example -
PS C:\> Get-Content "C:\Logs\AppLog.txt" -Tail 10

A -Wait option of the Get-Content command in PowerShell is an equivalent of tail -f on Linux systems:

PS C:\> Get-Content <fileName> -Wait
- example -
PS C:\> Get-Content "C:\Logs\AppLog.txt" -Wait

The same as tail -f on Linux, the above command will print the contents of a file and then will continue printing lines as they are added to the file unless it is interrupted with, for example, Ctrl + C keystroke.

PowerShell Head Command

To print a head of the output of a PowerShell command in Windows, execute:

PS C:\> <command>| Select -First <numberOfLines>
- example -
PS C:\> systeminfo | Select -First 10

To print the first few number of lines of a file in Windows, use a combination of the PowerShell Get-Content command with the -Head option:

PS C:\> Get-Content <fileName> -Head <numberOfLines>
- example -
PS C:\> Get-Content "C:\Logs\AppLog.txt" -Head 10

Cool Tip: Windows touch command equivalent in CMD and PowerShell! Read more →

Was it useful? Share this post with the world!

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK