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.
Many of my PowerShell v2.0 scripts use comment blocks like this:
|
1 |
<<span style="color: #008000">#</span> |
|
1 |
.Synopsis |
|
1 |
Pipe PowerShell output to a temp file and open the file <span style="color: #0000ff">in</span> Notepad or an editor of your choice. |
|
1 |
.Description |
|
1 |
This <span style="color: #0000ff">function</span> creates a temporary file from pipelined input and opens <span style="color: #0000ff">in</span> <span style="color: #0000ff">in</span> a text editor of |
|
1 |
your choice. The <span style="color: #0000ff">default</span> is Notepad.exe but you can use -editor to specify the filename and path |
|
1 |
<span style="color: #0000ff">for</span> any text editor. The temp file will be deleted after closing the text editor. If you want to |
|
1 |
save the output you'll need to give it a new name. |
|
1 |
.Parameter InputObject |
|
1 |
Any pipelined input |
|
1 |
.Parameter Editor |
|
1 |
The file name and path, <span style="color: #0000ff">if</span> necessary, to any text editor of your choice. You can also use any |
|
1 |
command aliases that you have defined. |
|
1 |
.Example |
|
1 |
PS C:\> ps | out-notepad |
|
1 |
Take the output from Get-Process and send it to Notepad. |
|
1 |
.Example |
|
1 |
PS C:\> get-wmiobject win32_logicaldisk | Select * | out-notepad -editor nplus |
|
1 |
Output from the Get-WMIObject expression is sent to the temp file and opened with Notepad++ |
|
1 |
which has been defined with an alias of nplus |
|
1 |
.Inputs |
|
1 |
Accepts pipelined input |
|
1 |
.Outputs |
|
1 |
None |
|
1 |
.Link |
|
1 |
http://jdhitsolutions.com/blog |
|
1 |
.Link |
|
1 |
Out-File |
|
1 |
Out-Printer |
|
1 |
OUt-GridView |
|
1 |
.Notes |
|
1 |
NAME: Out-Notepad |
|
1 |
VERSION: 2.1 |
|
1 |
AUTHOR: Jeffery Hicks http://jdhitsolutions.com/blog |
|
1 |
LASTEDIT: 10/18/2009 |
|
1 |
**************************************************************** |
|
1 |
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED * |
|
1 |
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF * |
|
1 |
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, * |
|
1 |
* DO NOT USE IT OUTSIDE OF A SECURE, TEST ENVIRONMENT. * |
|
1 |
**************************************************************** |
|
1 |
#> |
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.
|
1 |
highlight comment blocks <span style="color: #0000ff">in</span> PowerShell v2 scripts (ps1,psd1,psm1) |
|
1 |
<span style="color: #0000ff">if</span> ( ($_.Trim().startswith(<span style="color: #006080">"<#"</span>) -AND ((Get-Item $filename).Extension <span style="color: #cc6633">-match</span> <span style="color: #006080">"ps1|psd1|psm1"</span>)) -OR $blnCommentBlock) { |
|
1 |
<span style="color: #008000">#if the begin comment character is found turn "on" comment mode and treat</span> |
|
1 |
<span style="color: #008000">#every subsequent line as a comment until a matching #> is found</span> |
|
1 |
Write-Debug <span style="color: #006080">"Found a v2 comment block"</span> |
|
1 |
Write-Debug <span style="color: #006080">"Setting blnCommentBlock to TRUE"</span> |
|
1 |
Write-Debug <span style="color: #006080">"Setting isComment to TRUE"</span> |
|
1 |
Write-Debug <span style="color: #006080">"Setting fcolor to $CommentColor"</span> |
|
1 |
$blnCommentBlock=$True |
|
1 |
$isComment=$True |
|
1 |
$fcolor=$CommentColor |
|
1 |
} |
The function will treat every line as a comment until a #> is found, at which point the comment flag is turned off.
|
1 |
<span style="color: #0000ff">if</span> ($_.Trim().startswith(<span style="color: #006080">"#>"</span>)) { |
|
1 |
Write-Debug <span style="color: #006080">"Found end of v2 comment block"</span> |
|
1 |
Write-Debug <span style="color: #006080">"setting blnCommentblock to FALSE"</span> |
|
1 |
$blnCommentBlock=$False |
|
1 |
} |
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.
|
1 |
.Link |
|
1 |
http://jdhitsolutions.com/blog/2009/10/more-fun-with-get-numberedcontent/ |
|
1 |
.Link |
|
1 |
Get-Content |
|
1 |
Write-Host |
If you have any bugs or suggestions, I hope you’ll share them with me.
Download the latest version of Get-NumberedContent

Pingback: Tweets that mention Get-NumberedContent v2 | The Lonely Administrator -- Topsy.com
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.