#Requires -version 2.0 # Jeffery Hicks # http://jdhitsolutions.com/blog # follow on Twitter: http://twitter.com/JeffHicks # "Those who forget to script are doomed to repeat their work." # **************************************************************** # * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * # * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * # * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * # * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. * # **************************************************************** 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 and 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 Filename The filename and path of the text file to display. .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, unless you use -passThru in which case [string] objects are written to the pipeline. .Link http://jdhitsolutions.com/blog/2009/10/get-numberedcontent-v2/ .Link Get-Content Write-Host .Notes NAME: Get-NumberedContent VERSION: 2.1.5 AUTHOR: Jeffery Hicks http://jdhitsolutions.com/blog LASTEDIT: 10/21/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 { Write-Debug "Comment character is #CommentCharacter" Write-Debug "Comment color is $CommentColor" if ($NoComment) { Write-Debug "No comments"} if ($NoBlank) {Write-Debug "No blank lines"} if ($passthru) {Write-Debug "Passthru"} } #end Begin Process { if ($_) { $Filename=$_ } write-debug "Testing $filename" If (Test-Path $filename) { Write-Debug "Getting Fullname property" $FullName=(Get-Item $Filename).FullName Write-Debug "Fullname = $Fullname" $counter = 0 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. I can't simply use $content.count #because a 1 line file won't return a count. Measure-0bject #works better. Write-Debug "Calculating number of lines" $c=($content | Measure-Object).Count.ToString().Length write-debug "Padding line numbers to $c places" #Write scriptname Write-Debug "Writing filename $FullName" $line = "{0:d$($c)} | {1}" -f $counter,$FullName.ToUpper() $fcolor="White" if ($Passthru) { Write-Debug "writing line to pipeline" write $line } else { Write-Debug "Writing line to console" Write-Host $line -foregroundcolor $fcolor } 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 } #highlight comment blocks in PowerShell v2 scripts (ps1,psd1,psm1) if ( ($_.Trim().startswith("<#") -AND ((Get-Item $filename).Extension -match "ps1|psd1|psm1")) -OR $blnCommentBlock) { #if the begin comment character is found turn "on" comment mode and treat #every subsequent line as a comment until a matching #> is found Write-Debug "Found a v2 comment block" Write-Debug "Setting blnCommentBlock to TRUE" Write-Debug "Setting isComment to TRUE" Write-Debug "Setting fcolor to $CommentColor" $blnCommentBlock=$True $isComment=$True $fcolor=$CommentColor } else { Write-Debug "isComment is FALSE" $isComment=$False if ($_.Trim().StartsWith($CommentCharacter)) { write-debug "Setting isComment to TRUE" Write-Debug "Setting fcolor to $commentColor" $fcolor=$CommentColor $isComment=$True } } #end Else if ($_.Trim().startswith("#>")) { Write-Debug "Found end of v2 comment block" Write-Debug "setting blnCommentblock to FALSE" $blnCommentBlock=$False } if (($NoBlank -AND $Empty) -OR ($NoComment -AND $IsComment )) { write-debug "Skipping line" } else { #not a blank line $counter++ #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() Write-Debug "line is $line" if ($Passthru) { Write-Debug "writing line to pipeline" write $line } else { Write-Debug "Writing line to console" Write-Host $line -foregroundcolor $fcolor } } #else not a blank line } #end ForEach } #end if Test-Path else { Write-Debug "write warning failed to find $filename" Write-Warning "Failed to find $filename" } #write a blank line after each file is processed which makes output easier to read #if multiple files are processed write `n } #end Process End { Write-Debug "Ending and exiting function" } #end END } #end function Set-Alias gnc Get-NumberedContent