Prof. Powershell

A 'Tail' of Two Files

PowerShell has finally simplified the "tail" command line tool. Here's a look at how.

Monitoring log files is a pretty typical task for an IT Pro. Often we need to watch as new information is written to a file. In the past there have been any number of "tail" command line tools. This was difficult to do in PowerShell v2. Yes, we could get the last X number of lines in a file but there was no true tail equivalent. Fortunately in the latest version of PowerShell this oversight has been corrected with some enhancements to Get-Content.

PS C:\> get-content C:\windows\WindowsUpdate.log -tail 10

Now it is very easy to specify the last X number of lines to display as you can see in Figure 1.

Invoke-WebRequest cmdlet

Figure 1. (Click image to view larger version.)

To get the tail functionality, all we need to do is to include the –Wait parameter.

PS C:\> get-content C:\windows\WindowsUpdate.log -tail 1 –wait

As you can see in Figure 2, PowerShell is patiently waiting for changes to the file.

Invoke-WebRequest cmdlet

Figure 2. (Click image to view larger version.)

If I attempt to check for new Windows updates in another session, I'll see the new lines as they are added to the log file. Then PowerShell will keep waiting. Press Ctrl+C to break out.

Here's a cool trick with this command: pipe it to Out-GridView as you can see in Figure 3.

PS C:\> get-content C:\windows\WindowsUpdate.log -tail 1 -wait | out-gridview -title
"Windows Update Log"

 

Invoke-WebRequest cmdlet

Figure 3. (Click image to view larger version.)

As new lines are written to the log file you will see them in Out-Gridview. Again, press Ctrl+C in the console to terminate and then close Out-Gridview.

Finally, here's one more technique I came up with. I saw a question in the PowerShell Community on Google Plus. A member wanted to know how to use tail on multiple files at the same time. You can pipe multiple files to Get-Content and get the last X number of lines.

PS C:\> dir c:\work\*.txt -Include London.txt,Paris.txt | get-content -tail 1
I am in London
I am in Paris

There's no way to tell which line belongs to which file from the output, other than I made it easy for the demonstration. But if you try this, it will only watch the first file.

PS C:\> dir c:\work\*.txt -Include London.txt,Paris.txt | get-content -tail 1 –wait

Invoke-WebRequest cmdlet

Figure 4. (Click image to view larger version.)

As you can see in Figure 4 the only file that reports changes is the first one. But, because this only works in PowerShell v3, we could use a workflow and run the command in parallel!

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

foreach -parallel ($file in $path) {(
   Write-Verbose -message "Waiting for $file"
   Get-Content -path $file -tail 1 -wait {(
} #foreach

} #end workflow

With this workflow loaded in my PowerShell session, I can use it like this:

PS C:\> Get-Tail (dir c:\work\*.txt -Include London.txt,Paris.txt).Fullname –Verbose

You can't pipe objects to a workflow so I passed the file names as parameter values. But I now get both files displayed in the same console.

Invoke-WebRequest cmdlet

Figure 5. (Click image to view larger version.)

Again, you have to know what to expect with each file in order to determine which line goes where. But it works. And because workflows are really designed for remote management, I could use this workflow to easily put a tail on a file on a remote computer without having to change anything in the workflow. Remoting is built in!

PS C:\> Get-Tail (dir c:\work\*.txt -Include London.txt,Paris.txt).Fullname –Verbose

Invoke-WebRequest cmdlet

Figure 6. (Click image to view larger version.)

One thing to watch is that because the workflow writes a progress bar at the top of the screen, it could cover some of your output so hit Enter a few times as I've done. I haven't tested this technique to see how well it scales and since I can't tell what output goes with which computer (I could easily tail the same file on multiple computers) or file I'm not sure how well this would scale. But depending on the situation it just might. Workflow requires PowerShell v3 and we'll be covering this topic more in future lessons.

If you would like to know what else is new in PowerShell v3 that you can start using today, take a look at my new training course from Trainsignal: PowerShell v3 New Features

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events

0 AM
Live! 360 Orlando
November 17-22, 2024
TechMentor @ Microsoft HQ
August 11-15, 2025