SOLVED

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 {
@'
{0}
-----------
{1}
'@ -f $_.Name, (Get-Content -Tail 5 $_.FullName -Raw)
}

 

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

 

Please advise

2 Replies

@StevenAndler 

 

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:

LainRobertson_5-1654047791092.png

 

Example output with carriage returns:

LainRobertson_4-1654047751208.png

 

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 @"
$($_.Name);
---------------
$(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 {
        "$($_.Name)`n--------------";
        Get-Content -Path ($_.FullName) -Tail 5
    }

 

Output example from this method:

LainRobertson_3-1654047670026.png

 

Cheers,

Lain

 

Edited to simplify Option 2 a tad more.

best response confirmed by StevenAndler (Copper Contributor)
Solution

@LainRobertson 

 

Lain this is just what I was looking for.

 

Thank you very much!

 

1 best response

Accepted Solutions
best response confirmed by StevenAndler (Copper Contributor)
Solution

@LainRobertson 

 

Lain this is just what I was looking for.

 

Thank you very much!

 

View solution in original post