Windows PowerShell Commands to Achieve Unix Tail Command
-
tail
in Win 32 to Achievetail
Unix Command Function -
Windows PowerShell With
Get-Content
to Achievetail
Unix Command Function -
Windows PowerShell With
Cat
to Achievetail
Unix Command Function
Unix Operating System contains the OS command tail
. The purpose of the tail
program is to display the tail end of a text file/piped data.
The expected output is the last lines of a file. The number of lines can be variated. The same command can be used with more than one file at once to print the standard output.
Besides Unix, the tail
command supports Unix-like Systems and FreeDOS and MSX-DOS systems.
In Windows PowerShell, the Windows version of a cross-platform application, command-line shell commands can be executed to achieve automation in different functionalities. However, the commands used here variate from standard scripting commands used in Unix-like systems.
There are several acceptable ways of doing a tail
similar function in Windows PowerShell.
tail
in Win 32 to Achieve tail
Unix Command Function
One of the common ways to achieve this functionality would be using the tail-f
program for Windows PowerShell; Tail for Win32. It tracks any file changes and allows the user to track changes in real-time. However, it requires users to use another program entirely, available through the link: http://tailforwin32.sourceforge.net. Therefore, it is not a suitable solution for a user who is searching for a PowerShell-specific alternative for the Windows System.
However, there are some acceptable PowerShell-based solutions for this as well.
Windows PowerShell With Get-Content
to Achieve tail
Unix Command Function
One most effective is to use Get-Content
. It is to be followed with -Tail n
, n being the number of lines you need to get as the output.
In the earlier versions of Windows Power shell, V1 and V2, this functionality was achieved through the Get-Content
command followed by the -Wait
command. If the file is changing in real-time, the Wait
command is used so that any changes can be tracked.
Get-Content .\DummyLogFile.txt
Here the output would get the content of the whole file as shown below. It starts from the very first lines of the log file and continues to the end.
2021-12-31 14:49:47, Info CBS TI: --- Initializing Trusted Installer ---
2021-12-31 14:49:47, Info CBS TI: Last boot time: 2021-12-31 12:41:44.516
2021-12-31 14:49:47, Info CBS Startup processing thread terminated normally
2021-12-31 14:49:47, Info CBS TI: Startup Processing completes, release startup processing lock.
.
.
However, real-time changes to file tail can be tracked when used with - Wait
.
Get-Content .\DummyLogFile.txt -Wait
Here, the output would have all the file data and wait for the changes below.
2021-12-31 14:49:47, Info CBS TI: --- Initializing Trusted Installer ---
2021-12-31 14:49:47, Info CBS TI: Last boot time: 2021-12-31 12:41:44.516
2021-12-31 14:49:47, Info CBS Startup processing thread terminated normally
2021-12-31 14:49:47, Info CBS TI: Startup Processing completes, release startup processing lock.
_
Later in the versions following V2 of PowerShell, V3 supports -Tail
keyword. Hence, you can preview only the required last lines rather than getting the whole file.
Get-Content .\DummyLogFile.txt -Tail 4
The output given is the last four lines of the given text. 4 is a variable value of n, following the tail command to include the number of tail lines below.
C:\Users> Get-Content .\DummyLogFile.txt -Tail 4
2022-01-06 08:58:10, Info CBS Ending the TrustedInstaller main loop.
2022-01-06 08:58:10, Info CBS Starting TrustedInstaller finalization.
2022-01-06 08:58:10, Info CBS Lock: Lock removed: WinlogonNotifyLock, level: 8, total lock:6
2022-01-06 08:58:10, Info CBS Ending TrustedInstaller finalization.
PS C:\Users>
When the user requires real-time data, like in a log file, a constantly changing file, it is possible to add the -Wait
command. When new last lines are added, the output gets updated to print any new lines.
PS C:\Users> Get-Content .\DummyLogFile.txt -Wait -Tail 4
The output would wait for changes.
Get-Content .\DummyLogFile.txt -Wait -Tail 4
2022-01-06 08:58:10, Info CBS Ending the TrustedInstaller main loop.
2022-01-06 08:58:10, Info CBS Starting TrustedInstaller finalization.
2022-01-06 08:58:10, Info CBS Lock: Lock removed: WinlogonNotifyLock, level: 8, total lock:6
2022-01-06 08:58:10, Info CBS Ending TrustedInstaller finalization.
However, when you use -wait
with a larger tail value, you let the system wait in a constantly changing file such as a log file. It results in a large memory consumption to occur. Hence, it is important to be mindful about the type of file you use -wait
-tail
commands together.
Windows PowerShell With Cat
to Achieve tail
Unix Command Function
It would be convenient for users who are more familiar with the Unix-like systems to use the cat
command.
PS C:\Users> cat .\DummyLogFile.txt
Using cat command only would output or print the content of the text file, similar to Get-Content
only.
2021-12-31 14:49:47, Info CBS TI: --- Initializing Trusted Installer ---
2021-12-31 14:49:47, Info CBS TI: Last boot time: 2021-12-31 12:41:44.516
2021-12-31 14:49:47, Info CBS Startup processing thread terminated normally
2021-12-31 14:49:47, Info CBS TI: Startup Processing completes, release startup processing lock.
...
However, using cat
with the -Tail n
command (n being the number of final lines required in the output) would provide a similar output to the tail command in Unix.
PS C:\Users> cat .\DummyLogFile.txt -Tail 4
The output of the code would be as follows.
C:\Users> cat .\DummyLogFile.txt -Tail 4
2022-01-06 08:58:10, Info CBS Ending the TrustedInstaller main loop.
2022-01-06 08:58:10, Info CBS Starting TrustedInstaller finalization.
2022-01-06 08:58:10, Info CBS Lock: Lock removed: WinlogonNotifyLock, level: 8, total lock:6
2022-01-06 08:58:10, Info CBS Ending TrustedInstaller finalization.
PS C:\Users>
When required, the cat
command can also be used with the Wait
command to track the real-time changes to the text files, like log files in a system.
PS C:\Users> cat .\DummyLogFile.txt -Tail 4 -Wait
Therefore, the output would be given, and the system waits for any changes in the given file/files. Then those changed file tails lines would be printed by the PowerShell as the output.
Get-Content .\DummyLogFile.txt -Wait -Tail 4
2022-01-06 08:58:10, Info CBS Ending the TrustedInstaller main loop.
2022-01-06 08:58:10, Info CBS Starting TrustedInstaller finalization.
2022-01-06 08:58:10, Info CBS Lock: Lock removed: WinlogonNotifyLock, level: 8, total lock:6
2022-01-06 08:58:10, Info CBS Ending TrustedInstaller finalization.
_
Linux tail
is most often used to track different log files in Linux and Linux-like systems. Therefore, PowerShell, which enables Windows users to run command-line scripts to achieve certain similar functions, is required to have such functionality.
In newer PowerShell versions, the tail
command is used along with the above described Get-Content
or cat
commands, and the wait
command is used to track any real-time changes.
Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.