How to Paste Formatted PowerShell Code with Line Numbers on TechNet Wiki

How to Paste Formatted PowerShell Code with Line Numbers on TechNet Wiki

The line numbers portion of this script is only really useful for displaying longer scripts. The line numbers are probably better suited for use in TechNet Gallery.

This is an extension to the article "How to Insert Formatted Windows PowerShell Code on TechNet Wiki". With this method, you will be able to past code with line numbers.



This can be helpful with longer scripts as people can reference a line number when asking questions or commenting.

Formatted code can be pasted from Windows PowerShell, the Windows PowerShell ISE or PowerGUI Script Editor from Quest Software.

First, you will need to get the module from powergui.org, and place it into your module folder.

Once the module is downloaded, you will need to load it into your script editor or Windows PowerShell.
 

In Windows PowerShell

  1. Import the module > Import-Module Add-on.CopyAsColorizedHTML
  2. Run the command Copy-ColorizedHTML with a file path:
    Copy-ColorizedHTML -path .\test-me.ps1
  3. Paste the results to the Wiki:
    001
    002
    003
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    [string]$LogName = Join-Path (Split-Path $Invocation.MyCommand.Path) "$group.log"
    write-host $LogName
        

In Windows PowerShell ISE

  1. Either import the module as above, or add it to the Microsoft.PowerShellISE_profile.ps1 script:
    $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(“Copy As Colorized HTML”,{Copy-ColorizedHTML},“Ctrl+Shift+C”) | Out-Null
  2. Load the script you want to copy in to the ISE (Either by typing it in, or from a file)
  3. If you want to copy a portion of the script, select thhat portion
  4. Press Ctrl+Shift+C to copy the contents of the ISE to your clipboard in HTML format
  5. Paste the result to the Wiki:
    001
    002
    003
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    [string]$LogName = Join-Path (Split-Path $Invocation.MyCommand.Path) "$group.log"
    write-host $LogName

In PowerGUI

  1. In the File / PowerShell Libraries dialog box, select Add-on.CopyAsColorizedHTML.
  2. Load the script you want to copy in to the editor (Either by typing it in, or from a file)
  3. If you want to copy a portion of the script, select thhat portion
  4. In Edit menu click Copy As and click Colorized HTML to copy the contents of the script editor to your clipboard in HTML format
  5. Paste the result to the Wiki:
    001
    002
    003
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    [string]$LogName = Join-Path (Split-Path $Invocation.MyCommand.Path) "$group.log"
    write-host $LogName
Leave a Comment
  • Please add 7 and 1 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Richard Mueller edited Revision 5. Comment: Replace RGB values with color names in HTML to restore colors

  • Craig Lussier edited Revision 3. Comment: added en-US to tags and title

  • Ed Price - MSFT edited Revision 2. Comment: Added the first sentence per Rich's comment and the TOC.

  • Ed Price - MSFT edited Original. Comment: Title casing. Looks good!

Page 1 of 1 (4 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Ed Price - MSFT edited Original. Comment: Title casing. Looks good!

  • I like the idea of being able to copy/paste colorized code and line numbers, but I am not sure if the line numbers portion should be used on the Wiki.  As you mentioned, it would only really be useful for longer scripts and that would probably be better suited for the TechNet Gallery.

    If you wanted to discuss a certain aspect of a script on the Gallery, you could ask the owner a question on the Gallery contribution. If it is your script and you wanted to explain the technique that you used, you could post that portion on the Wiki page and then discuss the snippet (which would not require a long script with line numbers).

    Then again, I could be forgetting a Wiki use-case completely.  I do, however, see many other uses for that add-on, including the troubleshooting of script errors with others.

  • Ed Price - MSFT edited Revision 2. Comment: Added the first sentence per Rich's comment and the TOC.

  • Craig Lussier edited Revision 3. Comment: added en-US to tags and title

  • Richard Mueller edited Revision 4. Comment: Removed (en-US) from title, added tags

  • Richard Mueller edited Revision 5. Comment: Replace RGB values with color names in HTML to restore colors

Page 1 of 1 (6 items)