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.
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.
|
1 |
Function Get-NumberedContent { |
|
1 |
<span style="color: #008000">#Requires -version 2.0</span> |
|
1 |
  |
|
1 |
<<span style="color: #008000">#</span> |
|
1 |
.Synopsis |
|
1 |
Display file contents <span style="color: #0000ff">in</span> a numbered fashion. |
|
1 |
.Description |
|
1 |
This <span style="color: #0000ff">function</span> will display the contents of a text file as numbered output. If the file is |
|
1 |
a <span style="color: #0000ff">script</span> file, commented lines will be displayed <span style="color: #0000ff">in</span> Green. Unlike Get-Content, the output |
|
1 |
is written to the console using Write-Host. This <span style="color: #0000ff">function</span> is primarily meant as a console |
|
1 |
based file viewer.It does not write to the pipeline unless you use the -PassThru parameter |
|
1 |
<span style="color: #0000ff">in</span> which case you will get no colorized output. |
|
1 |
|
1 |
For <span style="color: #0000ff">script</span> files, or any file <span style="color: #0000ff">for</span> that matter, you can specify a character ad the comment |
|
1 |
character. The <span style="color: #0000ff">default</span> comment character is the <span style="color: #008000">#. Any line that begins with that chara-</span> |
|
1 |
cter will be treated as a comment. You can skip comments by using -NoComment. Otherwise |
|
1 |
the line will print <span style="color: #0000ff">in</span> a green font. You can override the fontcolor using -CommentColor. |
|
1 |
|
1 |
Use -NoBlank to suppress output of any blank lines. You can also combine -NoBlank and |
|
1 |
-NoComment to get a very short numbered line output. |
|
1 |
|
1 |
Line 0 will display the full filename and path |
|
1 |
|
1 |
.Parameter Filepath |
|
1 |
The filename and path. |
|
1 |
.Parameter CommentCharacter |
|
1 |
The character to use as the comment character. The <span style="color: #0000ff">default</span> is <span style="color: #008000">#. The parameter has an </span> |
|
1 |
alias of <span style="color: #006080">"Char"</span>. |
|
1 |
.Parameter CommentColor |
|
1 |
The font color to use <span style="color: #0000ff">for</span> commented lines. The <span style="color: #0000ff">default</span> is green. This parameter has an |
|
1 |
alias of <span style="color: #006080">"Color"</span> |
|
1 |
.Parameter NoComments |
|
1 |
If the file is a <span style="color: #0000ff">script</span> file, -NoComments will suppress any lines that <span style="color: #0000ff">begin</span> with the |
|
1 |
appropriate comment character. |
|
1 |
.Parameter NoBlanks |
|
1 |
Suppress output of any blank lines. Line tabs and spacing will be maintained but blank |
|
1 |
lines will not be displayed. |
|
1 |
.Parameter Passthru |
|
1 |
Write the output to the pipeline. |
|
1 |
.Example |
|
1 |
PS C:\> Get-NumberedContent c:\scripts\test.ps1 |
|
1 |
  |
|
1 |
Display line numbered content of Test.ps1 using the <span style="color: #0000ff">default</span> comment character (<span style="color: #008000">#) and the</span> |
|
1 |
<span style="color: #0000ff">default</span> comment color, Green. |
|
1 |
.Example |
|
1 |
PS C:\> Get-NumberedContent c:\scripts\update.vbs -nocomment -char <span style="color: #006080">"'"</span> |
|
1 |
|
1 |
Display the results of update.vbs without and lines that start with the comment character |
|
1 |
<span style="color: #0000ff">for</span> VBS scripts. This expression is using the parameter alias CHAR <span style="color: #0000ff">for</span> -CommentCharacter. |
|
1 |
.Example |
|
1 |
PS C:\> get-numberedcontent c:\files\report.ext -noblanks -pass | out-file NumReport.txt |
|
1 |
|
1 |
Display the contents of c:\files\report.txt without any blank lines and pass to the pipeline. |
|
1 |
The pipelined output is then sent to the Out-File cmdlet. |
|
1 |
.Example |
|
1 |
PS C:\> dir c:\TEST\*.CSV | get-numberedcontent -commentCharacter <span style="color: #006080">";"</span> -commentColor <span style="color: #006080">"Red"</span> -noblanks |
|
1 |
|
1 |
Get the content <span style="color: #0000ff">for</span> every CSV file <span style="color: #0000ff">in</span> the Test directory. Commented lines that start with ; |
|
1 |
will be displayed <span style="color: #0000ff">in</span> a red color and blank lines will be suppressed. |
|
1 |
|
1 |
.Inputs |
|
1 |
Accepts strings as pipelined input |
|
1 |
.Outputs |
|
1 |
None |
|
1 |
|
1 |
.Link |
|
1 |
Get-Content |
|
1 |
  |
|
1 |
|
1 |
.Notes |
|
1 |
NAME: Get-NumberedContent |
|
1 |
VERSION: 2.0 |
|
1 |
AUTHOR: Jeffery Hicks |
|
1 |
http://jdhitsolutions.com/blog |
|
1 |
LASTEDIT: 10/13/2009 |
|
1 |
  |
|
1 |
  |
|
1 |
<span style="color: #008000">#></span> |
|
1 |
  |
|
1 |
  |
|
1 |
[CmdletBinding()] |
|
1 |
  |
|
1 |
<span style="color: #0000ff">param</span> ( |
|
1 |
[Parameter( |
|
1 |
ValueFromPipeline=$True, |
|
1 |
Position=0, |
|
1 |
Mandatory=$True, |
|
1 |
HelpMessage=<span style="color: #006080">"The filename and path of a text file."</span>)] |
|
1 |
[string]$Filename, |
|
1 |
|
1 |
[Parameter( |
|
1 |
ValueFromPipeline=$False, |
|
1 |
Mandatory=$False, |
|
1 |
HelpMessage=<span style="color: #006080">"The comment character for a specific file type."</span>)] |
|
1 |
[Alias(<span style="color: #006080">"Char"</span>)] |
|
1 |
[string]$CommentCharacter=<span style="color: #006080">"#"</span>, |
|
1 |
|
1 |
[Parameter( |
|
1 |
ValueFromPipeline=$False, |
|
1 |
Mandatory=$False, |
|
1 |
HelpMessage=<span style="color: #006080">"The comment character color. Default is Green."</span>)] |
|
1 |
[ValidateSet(<span style="color: #006080">"Black"</span>,<span style="color: #006080">"DarkBlue"</span>,<span style="color: #006080">"Blue"</span>,<span style="color: #006080">"DarkGreen"</span>,<span style="color: #006080">"Green"</span>,<span style="color: #006080">"DarkCyan"</span>,<span style="color: #006080">"Cyan"</span>, |
|
1 |
<span style="color: #006080">"DarkRed"</span>,<span style="color: #006080">"Red"</span>,<span style="color: #006080">"Magenta"</span>,<span style="color: #006080">"White"</span>,<span style="color: #006080">"DarkGray"</span>,<span style="color: #006080">"Gray"</span>,<span style="color: #006080">"DarkYellow"</span>,<span style="color: #006080">"Yellow"</span>)] |
|
1 |
[Alias(<span style="color: #006080">"Color"</span>)] |
|
1 |
[string]$CommentColor=<span style="color: #006080">"Green"</span>, |
|
1 |
|
1 |
[Parameter( |
|
1 |
ValueFromPipeline=$False, |
|
1 |
Mandatory=$False, |
|
1 |
HelpMessage=<span style="color: #006080">"Suppress comment lines for script files."</span>)] |
|
1 |
[<span style="color: #0000ff">switch</span>]$NoComment, |
|
1 |
|
1 |
[Parameter( |
|
1 |
ValueFromPipeline=$False, |
|
1 |
Mandatory=$False, |
|
1 |
HelpMessage=<span style="color: #006080">"Suppress blank lines."</span>)] |
|
1 |
[<span style="color: #0000ff">switch</span>]$NoBlank, |
|
1 |
|
1 |
[Parameter( |
|
1 |
ValueFromPipeline=$False, |
|
1 |
Mandatory=$False, |
|
1 |
HelpMessage=<span style="color: #006080">"Write object to the pipeline instead of the console."</span>)] |
|
1 |
[<span style="color: #0000ff">switch</span>]$Passthru |
|
1 |
|
1 |
) |
|
1 |
  |
|
1 |
Begin { |
|
1 |
<span style="color: #0000ff">if</span> ($NoComment) { Write-Debug <span style="color: #006080">"No comments"</span>} |
|
1 |
<span style="color: #0000ff">if</span> ($NoBlank) {Write-Debug <span style="color: #006080">"No blank lines"</span>} |
|
1 |
Write-Debug <span style="color: #006080">"Comment character is #CommentCharacter"</span> |
|
1 |
Write-Debug <span style="color: #006080">"Comment color is $CommentColor"</span> |
|
1 |
<span style="color: #0000ff">if</span> ($passthru) {Write-Debug <span style="color: #006080">"Passthru"</span>} |
|
1 |
|
1 |
} <span style="color: #008000">#end Begin</span> |
|
1 |
|
1 |
Process { |
|
1 |
  |
|
1 |
<span style="color: #0000ff">if</span> ($_) { |
|
1 |
$Filename=$_ |
|
1 |
$FullName=$_.Fullname |
|
1 |
} |
|
1 |
<span style="color: #0000ff">else</span> { |
|
1 |
$Fullname=$Filename |
|
1 |
} |
|
1 |
|
1 |
write-debug <span style="color: #006080">"Testing $filename"</span> |
|
1 |
If (Test-Path $filename) { |
|
1 |
$counter = -1 |
|
1 |
|
1 |
write-debug <span style="color: #006080">"Getting content"</span> |
|
1 |
$content=get-content $Filename |
|
1 |
|
1 |
<span style="color: #008000">#get the total number of lines and then the length</span> |
|
1 |
<span style="color: #008000">#of that number so the number of leading zeros can be</span> |
|
1 |
<span style="color: #008000">#calculated more accurately</span> |
|
1 |
write-debug <span style="color: #006080">"Calculating number of lines"</span> |
|
1 |
$c=($content.count).ToSTring().Length |
|
1 |
|
1 |
write-debug <span style="color: #006080">"Padding line numbers to $c places"</span> |
|
1 |
write-debug <span style="color: #006080">"Processing content"</span> |
|
1 |
$content | <span style="color: #0000ff">foreach</span> { |
|
1 |
<span style="color: #008000">#default font color</span> |
|
1 |
$fcolor=<span style="color: #006080">"White"</span> |
|
1 |
|
1 |
<span style="color: #008000">#determine if line is a blank</span> |
|
1 |
<span style="color: #0000ff">if</span> ($_.Trim().Length <span style="color: #cc6633">-gt</span> 0) { |
|
1 |
$Empty=$False |
|
1 |
write-debug <span style="color: #006080">"Line is not empty"</span> |
|
1 |
} |
|
1 |
<span style="color: #0000ff">else</span> { |
|
1 |
write-debug <span style="color: #006080">"Line is empty"</span> |
|
1 |
$Empty=$True |
|
1 |
} |
|
1 |
|
1 |
|
1 |
<span style="color: #008000">#determine if line is a comment</span> |
|
1 |
|
1 |
$isComment=$False |
|
1 |
|
1 |
<span style="color: #0000ff">if</span> ($_.Trim().StartsWith($CommentCharacter)) { |
|
1 |
write-debug <span style="color: #006080">"Comment line found"</span> |
|
1 |
$fcolor=$CommentColor |
|
1 |
$isComment=$True |
|
1 |
} |
|
1 |
|
1 |
|
1 |
<span style="color: #0000ff">if</span> (($NoBlank -AND $Empty) -OR ($NoComment -AND $IsComment )) { |
|
1 |
write-debug <span style="color: #006080">"Skipping line"</span> |
|
1 |
} |
|
1 |
|
1 |
<span style="color: #0000ff">else</span> { |
|
1 |
  |
|
1 |
$counter++ |
|
1 |
|
1 |
<span style="color: #0000ff">if</span> ($counter <span style="color: #cc6633">-eq</span> 0) { |
|
1 |
$line = <span style="color: #006080">"{0:d$($c)} | {1}"</span> -f $counter,$FullName.ToUpper() |
|
1 |
$fcolor=<span style="color: #006080">"White"</span> |
|
1 |
} |
|
1 |
|
1 |
<span style="color: #0000ff">else</span> { |
|
1 |
|
1 |
<span style="color: #008000">#write a line number with leading zeros the | bar and then the line of text from the file</span> |
|
1 |
<span style="color: #008000">#trimming off any trailing spaces</span> |
|
1 |
$line = <span style="color: #006080">"{0:d$($c)} | {1}"</span> -f $counter,$_.TrimEnd() |
|
1 |
} |
|
1 |
|
1 |
<span style="color: #0000ff">if</span> ($Passthru) { |
|
1 |
write $line |
|
1 |
} |
|
1 |
<span style="color: #0000ff">else</span> { |
|
1 |
Write-Host $line -foregroundcolor $fcolor |
|
1 |
} |
|
1 |
} <span style="color: #008000">#else not a blank line</span> |
|
1 |
|
1 |
|
1 |
} <span style="color: #008000">#end ForEach</span> |
|
1 |
} <span style="color: #008000">#end if Test-Path</span> |
|
1 |
<span style="color: #0000ff">else</span> { |
|
1 |
Write-Warning <span style="color: #006080">"Failed to find $filename"</span> |
|
1 |
} |
|
1 |
} <span style="color: #008000">#end Process</span> |
|
1 |
|
1 |
End { |
|
1 |
Write-Debug <span style="color: #006080">"Ending and exiting"</span> |
|
1 |
|
1 |
} |
|
1 |
|
1 |
} <span style="color: #008000">#end function</span> |
|
1 |
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?
