Examine the tail of multiple files using Get-Content

Copper Contributor

I am trying to examine the tail of all files in a folder that start with SMRT


here's my script

Get-ChildItem SMRT.* | ForEach-Object {
'@ -f $_.Name, (Get-Content -Tail 5 $_.FullName -Raw)


The above script runs without errors but doesn't display any information


Please advise

2 Replies



Hi, Steve.


Here's two options. Personally, I feel the "here-string" approach is a bit messy and contains unnecessary complexity meaning I'd opt for the second approach.


I've made the assumption that you want the five tail lines on separate lines, not crammed together on one line - which is what you get with your current "here-string" approach. You need to bake in the carriage returns as shown in Option 1.


Example output without carriage returns:



Example output with carriage returns:



Even here, the formatting is slightly out for the final four tail lines for reasons I won't go into. As I say, it's just messy and complex.


Let me know if I've made an incorrect assumption.


Option 1: The "here-string" approach


Get-ChildItem -Filter "SMRT.*" |
    ForEach-Object {
        Write-Host @"
$(Get-Content -Path ($_.FullName) -Tail 5 | ForEach-Object { "$_`n" })


The output is as shown above in the "with carriage returns" example.


Option 2: Basic approach


Get-ChildItem -Filter "SMRT.*" |
    ForEach-Object {
        Get-Content -Path ($_.FullName) -Tail 5


Output example from this method:






Edited to simplify Option 2 a tad more.

best response confirmed by StevenAndler (Copper Contributor)



Lain this is just what I was looking for.


Thank you very much!