Search code examples
windowspowershelltail

Why "Get-Content" is not refreshing contents live?


I have a logfile and used the command Get-Content myLog.log –Wait to show the contents of this file, just like with the linux equivalent tail.

I noticed that the content is not updated live like I know it from tail. The content is only refreshed after a specific interval. How can I change the interval, I think I have to pass an argument for Wait so that it knows how long to wait?


Solution

  • The Get-Content cmdlet doesn't expose a property nor a function to set the refresh intervall. You probably have to do it yourself:

    $linesPrinted = 0;
    
    while ($true) 
    { 
        $content = Get-Content myLog.log
        $currentLineCount = $content | Measure-Object -Line | select -expand Lines
    
        if ($currentLineCount -gt $linesPrinted)
        {
            $content[$linesPrinted .. $currentLineCount]
            $linesPrinted = $currentLineCount
        }
    
    
        Sleep -Milliseconds 100 
    }