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.
To split long command into multiple lines, use backtick (`) character in the command where you want to split it to multiple lines.
Just like the old command-line interpreter, PowerShell offers a more command. Its usage is as simple as its functionality. You can pass either the contents of a file or the output of a cmdlet through a pipe to the more command. You can also use the -Paths parameter to specify the files that you want to display.
Entering sl ~ is just like opening the folder called “Users” and from there “YOURUSERNAME” using your GUI. Let's start by learning how to move around between directories and view their contents.
Get-Content
(alias: gc
) is your usual option for reading a text file. You can then filter further:
gc log.txt | select -first 10 # head
gc -TotalCount 10 log.txt # also head
gc log.txt | select -last 10 # tail
gc -Tail 10 log.txt # also tail (since PSv3), also much faster than above option
gc log.txt | more # or less if you have it installed
gc log.txt | %{ $_ -replace '\d+', '($0)' } # sed
This works well enough for small files, larger ones (more than a few MiB) are probably a bit slow.
The PowerShell Community Extensions include some cmdlets for specialised file stuff (e.g. Get-FileTail).
Here are the built-in ways to do head
and tail
. Don't use pipes because if you have a large file, it will be extremely slow. Using these built-in options will be extremely fast even for huge files.
gc log.txt -head 10
gc log.txt -tail 10
gc log.txt -tail 10 -wait # equivalent to tail -f
more.exe
exists on Windows, ports of less
are easily found (and the PowerShell Community Extensions, PSCX, includes one).
PowerShell doesn't really provide any alternative to separate programs for either, but for structured data Out-Grid
can be helpful.
Head
and Tail
can both be emulated with Select-Object
using the -First
and -Last
parameters respectively.
Sed
functions are all available but structured rather differently. The filtering options are available in Where-Object
(or via Foreach-Object
and some state for ranges). Other, transforming, operations can be done with Select-Object
and Foreach-Object
.
However as PowerShell passes (.NET) objects – with all their typed structure, eg. dates remain DateTime
instances – rather than just strings, which each command needs to parse itself, much of sed
and other such programs are redundant.
"-TotalCount" in this instance responds exactly like "-head". You have to use -TotalCount or -head to run the command like that. But -TotalCount is misleading - it does not work in ACTUALLY giving you ANY counts...
gc -TotalCount 25 C:\scripts\logs\robocopy_report.txt
The above script, tested in PS 5.1 is the SAME response as below...
gc -head 25 C:\scripts\logs\robocopy_report.txt
So then just use '-head 25" already!
If you need to query large (or small) log files on Windows, the best tool I have found is Microsoft's free Log Parser 2.2. You can call it from PowerShell if you want and it will do all the heavy lifting for you, and very fast too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With