Home > Microsoft, PowerShell, Windows 7, Windows Server > PowerShell – Easy Line Numbers using Cat (Get-Content)

PowerShell – Easy Line Numbers using Cat (Get-Content)

October 8, 2009

I often cat out code pages from the prompt when I simply want to see what’s inside or look for a method.  Cat, of course, is an alias to PowerShell’s get-content cmdlet, but cat brings me back to days in Unix and makes me feel better.🙂

One thing that was a bit of a trouble was looking for a specific line of code.

For example: Looking through the debug logs, I see an error occuring at line 40 of some file.  That’s great, but who wants to count lines?  Instead, we open up Visual Studio and look.  Well, that’s a hassle too.

Here’s a quick script that does a simple line count with the line in the file.

param ([string] $filename)
$counter = 0;
$content = get-content $filename |
 % { $counter++; write-host “$counter`t| $_” }

Since the result of get-content becomes an enumerable line-by-line list, it’s easy to iterate through and increment a counter.

To call it, just call the method with the file name.  For mine, I called it cat-code.ps1, though I’ll probably set an alias to ‘cc’ or something later on.

.\cat-code.ps1 psake_default.ps1

Code Cat example.

From here, you could add in a check to the $Host.UI.RawUI and handle the overflows a bit better, but that’s for another day!

  1. October 9, 2009 at 1:10 pm

    I hope you don’t mind but I took your idea and ran with it a little.
    http://jdhitsolutions.com/blog/2009/10/get-numberedcontent/

  1. October 9, 2009 at 1:08 pm
Comments are closed.
%d bloggers like this: