9

I can tail one file via the following command:

Get-Content -Path C:\log1.txt -Tail 10 –Wait

How do I extend this to multiple files, I have tried the following with no luck:

Get-Content -Path C:\log1.txt,C:\log2.txt -Tail 10 –Wait

This will only pick up updates from the first file, not the second.

1
  • 3
    The -wait switch is going to block the thread. To -tail -wait multiple files at once, you're going to have to have multiple threads.
    – mjolinor
    Feb 17, 2015 at 18:07

3 Answers 3

10

Based on @mjolinor's comment, I have come up with the following that appears to work,

Workflow My-Tail
{
    Param([string[]] $Path)

    foreach -parallel ($file in $path)
    {
        Get-Content -Path $file -Tail 1 -Wait
    }
}

My-Tail (dir C:\*.log -Include log1.txt,log2.txt)

However, this has some sort of progress bar that appears...

2
  • 2
    You can add $ProgressPreference='SilentlyContinue' in the script to suppress the progress bar. I'm working on my own version that will also highlight and filter lines. github.com/McLeopold/TailorPosh
    – McLeopold
    Feb 16, 2017 at 16:18
  • A workflow cannot use recursion. + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : RecursiveWorkflowNotSupported
    – CuriousDev
    Dec 29, 2020 at 10:35
1

I can't speak to how efficient this is, but since I'm using PowerShell Core 7.1.3, I can't use Workflows or ForEach -Parallel, but I can use ForEach-Object -Parallel, so I tried it just to see what would happen...

gci -Path C:\ -Filter log*.txt |
    % -Parallel {
        cat -Wait -Tail 10 -Path $_
    } -ThrottleLimit 30

In my case, I had 27 files I needed to monitor, so I chose a number just above that, and this seemed to work.

Just to be sure it was working, I used this, which will output the source file name before each line:

gci -Path C:\ -Filter log*.txt |
    % -Parallel {
        $file = $_;
        cat -Wait -Tail 10 -Path $file |
            % { write "$($file.Name): ${_}" }
    } -ThrottleLimit 30
0

I needed tailed output across multiple files and I wanted to try do it in one line,
here's what I eventually came up with:

gci *.txt -recurse | ForEach-Object  { Write-Output "$_`n" + $(Get-Content $_ -tail 5) + "`n" }

Its takes a recursive directory listing of all files named *.txt,
writes the file path to console,
then writes the last 5 lines to console.

I didn't need to follow the tails of the files, they weren't being actively written to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.