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