Refactored Codes
How read tail content or end content or streaming logs in windows using powershell
In linux, we can use Tail command to read the newly added contents of a file. We run tail command on a file, and it shows the logs generated and added in the file. You can run following command to get the same effect. You just need to replace the value of -Path to the file.
Get-Content -Path .\Test.log -Tail 10 -Wait
The above command will show you 10 lines, if the file already had any content. Otherwise, it will keep waiting for new content, and will display as new line comes in. You can run tests to see it for yourself or you can see me working on it in the video. [embed]https://youtu.be/dgnBGIGmcJs[/embed] First, create a new file for testing.
New-item -ItemType File -Name Test.log
Then run the command to read the tail logs.
Get-Content -Path .\Test.log -Tail 10 -Wait
And, run the below for-loop to add new content every 1 second.
for($Count = 0; $Count -lt 1000; ++$Count) {
  $Content = "Log Line Number $Count";
  Write-Host "New line number $Count added to file -> " -NoNewline;
  Write-Host -Object $Content;
  Add-Content -Path .\Test.log -Value $Content;
  Start-Sleep -Seconds 1;
}