brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Get-Content -Tail 20 -Wait follows a log file in PowerShell like tail -f

Windows
~/tips/tip-windows-get-content-tail-wait

Get-Content -Path C:\Logs\app.log -Tail 20 -Wait

Shows the last lines of a file and keeps printing new ones as they are written.

On Linux I’d type tail -f to watch a log. PowerShell has the same trick built into Get-Content, and it is easy to forget because the parameters have different names.

Show the last 20 lines and keep waiting for new ones:

Get-Content -Path C:\Logs\app.log -Tail 20 -Wait

-Tail (alias -Last) picks the number of lines from the end. -Wait keeps the file open and checks it once per second, printing new lines as they arrive. Press Ctrl+C to stop. You can combine it with a filter to follow only the interesting lines:

Get-Content C:\Logs\app.log -Tail 50 -Wait | Select-String "error"

Caveats:

  • -Wait cannot be combined with -Raw.
  • If the file is deleted while you wait, the command stops with a non-terminating error. That is relevant for logs that rotate by renaming or deleting the file, so you may need to start it again after a rotation.
  • It checks once per second, so output is slightly delayed compared to a real tail on Linux.
  • Do not use -Tail with a negative value, which is an error, and -Tail 0 returns no lines.
  • The encoding follows the PowerShell version: PowerShell 7 reads UTF-8 without a byte order mark by default, so a log in another encoding may show odd characters. Add -Encoding if needed.

The docs also say that getting only the last lines with -Tail is faster than reading a whole file into a variable and indexing it.

Further reading: Microsoft Learn: Get-Content

More Windows tips Win+Ctrl+C Win+Shift+Left arrow Win+Ctrl+Left arrow Alt+PrtScn Left Alt + Left Shift + Print Screen All Windows tips (33) All topics

Link