Monitoring text file changes in real-time with PowerShell and emulating the "tail -f" command

Posted: (EET/GMT+2)

 

If you come from Linux or Unix environments, you may be familiar with tail -f for watching a log file as it grows. PowerShell has a similar capability built in.

You can use Get-Content with the -Wait parameter to monitor a file in real time. This is very useful for things like monitoring application logs and seeing how they add content in real-time. Let's see an example:

Get-Content C:\Logs\application.log -Wait

This prints new lines as they are appended to the file, similar to the Linux/Unix tail -f command.

Often you also want to see the last few lines first. In that case, combine it with -Tail parameter:

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

This shows the last 20 lines and then continues streaming new ones.

This is useful when monitoring application logs, IIS logs, or build output while a process is running.

You can also pipe the output for filtering. For example:

Get-Content C:\Logs\application.log -Wait |
Where-Object { $_ -like "*error*" }

That continuously shows only lines containing the word "error".