Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Get-NumberedContent v2

Posted on October 22, 2009October 22, 2009

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.

Download the latest version of Get-NumberedContent


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

3 thoughts on “Get-NumberedContent v2”

  1. Pingback: Tweets that mention Get-NumberedContent v2 | The Lonely Administrator -- Topsy.com
  2. Darrin Henshaw says:
    October 22, 2009 at 11:04 am

    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!

  3. Jeffery Hicks says:
    October 22, 2009 at 12:07 pm

    Even Microsoft documentation has typos, if you can believe it! I’ll fix it and replace the file on the server.

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d