Protip March: Quickly viewing logs with Powershell

Wow it feels like it’s been forever since I have posted.  I have been crazy busy with work stuff and am just now getting caught up with everything and have enough room to poke my head above the water and breath again finally.  We had a massive overhaul of our data center in mid February (among other things) and I am finally getting all the loose ends tied up from that project, including our brand-spanking new test environment which I am super excited about and which I will post about in the not so distant future.

Here is proof of some of our efforts just in case you don’t believe me :)

dc1

dc4

dc5

Anyway, getting back on track, I just discovered a slick way in Powershell to mimic the functionality of tail and tail -f in the Linux world.  If you have ever used tail then you know it is a great tool for monitoring log files or quickly looking at the end of a piece of code for example.

With the trick I’m about to show you, the same can essentially be done in Windows.  However, there are a few caveats.  For one, the syntax is a little bit different (if you want to change this just set up an alias).  The Powershell equivalent relies on the Get-Content cmdlet with the -Tail and -Wait flags to accomplish this task.

So in the following example I have instructed Powershell to look at the last 30 lines of the uploadpic.ps1 file and using the -Wait flag it will be updated as the file gets appended to.

Get-Content -path .\uploadpic.ps1 -Tail 30 -Wait

If you don’t care about viewing the file live then you can remove the -Wait flag and Powershell will simply grab the last N number of lines where N is 30 in our example.  30 seems like a good enough number in our example and can obviously be changed depending on your needs.  Easy enough for what I need it for.

Get-Content -path .\uploadpic.ps1 -Tail 30

As I mentioned, I will be going into a little more detail about some of the things I learned from our data center rebuild that I feel were some great lessons and good things to know/be aware of.   Standby for new contents as I get back to writing more blog posts and getting back up to speed on the writing side of things.

About the Author: Josh Reichardt

Josh is the creator of this blog, a system administrator and a contributor to other technology communities such as /r/sysadmin and Ops School. You can also find him on Twitter and Facebook.