Wiki: How to Insert Formatted Windows PowerShell Code on TechNet Wiki

Wiki: How to Insert Formatted Windows PowerShell Code on TechNet Wiki

This topic is a how to.
Please keep it as clear and simple as possible. Avoid speculative discussions as well as deep dive into underlying mechanisms or related technologies.

You might want to insert formatted and colored code syntax (the colorized and formatted script code is easier to read on Web pages and stands out a lot more). This will help readers understand the syntax faster when they read it.

Inserting code into the Wiki-editor can be a little tricky if you want Windows PowerShell code syntax formatting. This article assumes that you are using Windows PowerShell ISE (that is a part of PowerShell V2) or PowerGUI Script Editor from Quest Software.

Using PowerShell ISE

  1. Click here to go to the PowerShellPack Web site (opens in a new window). Download and install PowerShellPack.
  2. Run the Windows PowerShell ISE editor.
  3. In Windows PowerShell ISE command prompt type the following command: Import-Module PowerShellPack.
  4. In the menu bar will appear new element named Add-Ons.
  5. In the Windows PowerShell ISE editor, open the already saved Windows PowerShell script or just type your code manually.
  6. Select a code part that you want to insert into the Wiki editor.
  7. In Add-Ons menu click IsePack, click Edit and then click Copy-ColoredAsHTML.
  8. Now the selected code is saved to clipboard in to HTML format.
  9. Return back to the Wiki editor.
  10. Click the HTML button in the toolbar and paste the HTML code into the window in the appropriate location.
  11. Paste HTML code from clipboard to editor.
  12. It is a good idea to use good fonts for pasting source code in the editor. Therefore change leading <pre> tag to the following:

    <pre style="font-family: consolas; font-size: 12px" class="PowerShellColorizedScript">

  13. The result should be something like this:
#####################################################################            
# Test-Me.ps1            
# Version 0.63            
#            
# This script just tests something            
#            
#####################################################################            
#requires -Version 2.0            
            
function Test-Me {            
[CmdletBinding()]            
 param (            
  [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]            
  [string]$Argument            
 )            
 Write-Host "You have passed the following argument to me:"            
 Write-Host $Argument            
}

 

Using PowerGUI

  1. Click here to go to web site http://powergui.org/downloads.jspa (opens in a new window). Download and install PowerGUI Script Editor
  2. Run PowerGUI Script Editor
  3. In PowerGUI Script Editor open already saved Windows PowerShell script or just type your code manually.
  4. Select code part which you want to insert into the Wiki editor
  5. In Edit menu click Copy As and click HTML
  6. Now selected code is saved to clipboard in to HTML format
  7. Return back to the Wiki editor
  8. Click the HTML button in the toolbar and paste the HTML code into the window in the appropriate location
  9. Paste HTML code from clipboard to editor
  10. It is a good idea to use good fonts for pasting source code in the editor. Therefore change leading <pre> tag to the following:

    <pre style="font-family: consolas; font-size: 12px">

  11. The result should be something like this:
#####################################################################
# Test-Me.ps1
# Version 0.63
#
# This script just tests something
#
#####################################################################
#requires -Version 2.0

function Test-Me {
[CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
        [string]$Argument
    )
    Write-Host "You have passed the following argument to me:"
    Write-Host $Argument
}


See Also


 

Other Languages

This article is also available in other languages:

Leave a Comment
  • Please add 3 and 7 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Benoit Jester edited Revision 25. Comment: Add french link

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

  • Richard Mueller edited Revision 20. Comment: Added TOC and tag

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

  • Fernando Lugão Veltem edited Revision 17. Comment: added link to translated article Como Inserir Código Formatado do Windows PowerShell no TechNet Wiki (pt-BR)

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

  • yottun8 edited Revision 14. Comment: Japanese article's link Added.

  • FZB edited Revision 13. Comment: other languages added

  • Karl Mitschke edited Revision 12. Comment: Minor spoelling issue (HTLM instead of HTML in PowerGUI section)

  • Ed Price MSFT edited Revision 8. Comment: Added "See Also" section.

Page 1 of 2 (12 items) 12
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
  • Vadims Podans edited Revision 3. Comment: removed 'work in progress' header

  • Before I learned how to do this, I would want to know WHY I would want to do this. Any insight?

  • I believe that colorized and formatted script code is more readable on web pages, rather unformatted and uncolored code.

  • Ed Price MSFT edited Revision 6. Comment: Added the missing "Why" statement at the top. And updated title casing.

  • Ed Price MSFT edited Revision 8. Comment: Added "See Also" section.

  • Thanks for adding that info!

  • Useful information.

    Do you know if such a method can be used for any other kind of script snippets?

  • > Do you know if such a method can be used for any other kind of script snippets?

    if you are using one of these script editors — yes.

  • Karl Mitschke edited Revision 12. Comment: Minor spoelling issue (HTLM instead of HTML in PowerGUI section)

  • FZB edited Revision 13. Comment: other languages added

  • yottun8 edited Revision 14. Comment: Japanese article's link Added.

  • As a techie, I appreciate options when it comes to stuff like this, but I gotta say, the PowerGUI code coloring scheme just doesn't look right to me after using the PSISE for so long.  As a standard, I would recommend anyone looking to post colored code to use the PowerShell ISE from Microsoft.

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

  • Fernando Lugão Veltem edited Revision 17. Comment: added link to translated article Como Inserir Código Formatado do Windows PowerShell no TechNet Wiki (pt-BR)

  • Good Article.

Page 1 of 2 (21 items) 12