Forum Discussion
StevenAndler
May 31, 2022Copper Contributor
Examine the tail of multiple files using Get-Content
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 -...
- Jun 02, 2022
LainRobertson
Jun 01, 2022Silver Contributor
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 @"
$($_.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:
Cheers,
Lain
Edited to simplify Option 2 a tad more.
- StevenAndlerJun 02, 2022Copper Contributor