More Fun with Get-NumberedContent

As much fun as the original Get-NumberedContent function was after using it for awhile I realized I had imposed some limitations. I also realized it needed to be more flexible. What if someone wanted to specify a different color or use a different comment character such as a ; in an ini file? I also thought of a few more useful features, so I went back to my script editor and came up with a PowerShell v2.0 version of the function.

The function can now be more like a cmdlet. I’ve defined a help block so that you can use Get-Help to learn how to use this function.

Help Get-NumberedContent

The function now takes a number of parameters so you can customize how the function operates.

PARAMETERS
    -Filename <String>

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?       true (ByValue)
        Accept wildcard characters?

    -CommentCharacter <String>
        The character to use as the comment character. The default is #. The parameter has an
        alias of "Char".

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?

    -CommentColor <String>
        The font color to use for commented lines. The default is green. This parameter has an
        alias of "Color"

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?

    -NoComment [<SwitchParameter>]

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?

    -NoBlank [<SwitchParameter>]

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?

    -Passthru [<SwitchParameter>]
        Write the output to the pipeline.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?       false
        Accept wildcard characters?

The function has a bit more flexibility and you can use it the way YOU want to. I’ve used several v2 parameter features such as alias and ValidatationSet.


This function will display the contents of a text file as numbered output. If the file is

a script file, commented lines will be displayed in Green. Unlike Get-Content, the output

is written to the console using Write-Host. This function is primarily meant as a console

based file viewer.It does not write to the pipeline unless you use the -PassThru parameter

in which case you will get no colorized output.  But now if you use –passtrhu you can get line numbered and filtered output which you can send to Out-File or Out-Printer.

For script files, or any file for that matter, you can specify a character ad the comment

character. The default comment character is the #. Any line that begins with that character will be treated as a comment. You can skip comments by using -NoComment. Otherwise the line will print in green. You can override the color using -CommentColor.

Use -NoBlank to suppress output of any blank lines. You can also combine -NoBlank and

-NoComment to get a very short numbered line output.

Line 0 will now display the filename which is handy when piping several objects to the function. Oh..did I mention the function accepts pipelined input now?

Get Numbered Content for a VBS script