I wasn’t completely satisfied with the updated version of my Get-NumberedContent function. You should still refer to the earlier post for details on how to use the function. But I had some issues with the previous version and realized there were a few bugs. I’ve since updated the Get-NumberedContent function.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Many of my PowerShell v2.0 scripts use comment blocks like this:
<#
.Synopsis
Pipe PowerShell output to a temp file and open the file in Notepad or an editor of your choice.
.Description
This function creates a temporary file from pipelined input and opens in in a text editor of
your choice. The default is Notepad.exe but you can use -editor to specify the filename and path
for any text editor. The temp file will be deleted after closing the text editor. If you want to
save the output you'll need to give it a new name.
.Parameter InputObject
Any pipelined input
.Parameter Editor
The file name and path, if necessary, to any text editor of your choice. You can also use any
command aliases that you have defined.
.Example
PS C:\> ps | out-notepad
Take the output from Get-Process and send it to Notepad.
.Example
PS C:\> get-wmiobject win32_logicaldisk | Select * | out-notepad -editor nplus
Output from the Get-WMIObject expression is sent to the temp file and opened with Notepad++
which has been defined with an alias of nplus
.Inputs
Accepts pipelined input
.Outputs
None
.Link
https://jdhitsolutions.com/blog
.Link
Out-File
Out-Printer
OUt-GridView
.Notes
NAME: Out-Notepad
VERSION: 2.1
AUTHOR: Jeffery Hicks https://jdhitsolutions.com/blog
LASTEDIT: 10/18/2009
****************************************************************
* 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 ENVIRONMENT. *
****************************************************************
#>
As written, my function couldn’t detect this as a comment. So I updated Get-NumberedContent to check PowerShell scripts for a <# and if found flip a flag that basically says, I’m in comment mode.
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
}
The function will treat every line as a comment until a #> is found, at which point the comment flag is turned off.
if ($_.Trim().startswith("#>")) {
Write-Debug "Found end of v2 comment block"
Write-Debug "setting blnCommentblock to FALSE"
$blnCommentBlock=$False
}
I also fixed a few bugs and tweaked some things. For example, now the full filename will be displayed as line 00. The function now writes a blank line after each file. This is helpful when running through a collection of files. Finally, I added an online link to the help section to link to this page. When creating links, the online link needs to be separate from any other links you want to display.
.Link
https://jdhitsolutions.com/blog/2009/10/more-fun-with-get-numberedcontent/
.Link
Get-Content
Write-Host
If you have any bugs or suggestions, I hope you'll share them with me.
Nice Jeff! I’m also a Linux guy, so this is the equivalent of cat -n, where it numbers the lines.
One little typo in the description I came across:
For script files, or any file for that matter, you can specify a character ad the comment character.
Think you meant as the comment character. Either way it’s loaded into my PS Profile now for future use!
Even Microsoft documentation has typos, if you can believe it! I’ll fix it and replace the file on the server.