Script snippets in PowerShell ISE

Screenshot 2014-03-06 22.09.07 There are many PowerShell editors out there on the Internet, varying from commercial products to good old Notepad. When I am creating a PowerShell script I like to use Windows PowerShell ISE editor. The Windows PowerShell Integrated Scripting Environment is the default  editor for Windows PowerShell. In Windows PowerShell ISE, you can run commands and write, test, and debug scripts. Included features are, tab completion, syntax coloring, selective execution, and context-sensitive help. It is al ready installed on most of Windows versions and it has al the features I like use when writing a PowerShell script. One of the most important features is the intellisense feature that auto complete the commands as you are typing them.

Screenshot 2014-03-06 22.35.37

Windows PowerShell ISE also included Snippets. Snippets are best described as templates or examples of some of many types of scripts you can create with PowerShell.

To see a list of the available snippets, press CTRL+J, or click menu Edit>Start Snippets.

ISESnippet01

It is also possible to create your own snippets.

Let me show you two examples of how you can create your own snippet for the PowerShell ISE Editor. The first Snippet I want to show creates the ‘Framework’ you need to create your own help text for your PowerShell script.

 

New-IseSnippet -Title Comment-BasedHelp -Description "A Template for comment-based help." -Text "<#
.SYNOPSIS
.Description
.Parameter
.Inputs
.Outputs
.Example
.Link
#>"

 

With the next script you can create a custom header for your scripts. This makes it easy to indentify who created the script and what is the purpose of the script.

 

New-IseSnippet -Title Custom-Header -Description "A standard layout for all project scripts" -Text "# +----------------------------------------------------------------------------+
# | File    : <filename_script>.ps1                                            |
# | Author  : Robin Plomp (PepperByte)                                       |
# | Version : 1.00                                                             |
# | Purpose :                    |
# |                                                                            |
# | Synopsis:                                                                  |
# | Usage : Run Citrix XenDesktop Controller                          |
# +----------------------------------------------------------------------------+
# | Maintenance History                                                        |
# | -------------------                                                        |
# | Name            Date       Version  Description                            |
# | ---------------------------------------------------------------------------+
# | Robin Plomp   11-03-2014 1.0      Initial Version                        |
# |                                                                            |
# |                                                                            |
# +----------------------------------------------------------------------------+
"

 

To see which custom snippets are present on the system you are are working you can use the cmdlet Get-ISESnippet

image

The PowerShell Snippets we just created are saved on the following location:

C:\Users\%username%\Documents\WindowsPowerShell\Snippets

The snippets are saved with the ps1xml extension. The content of this file looks like this.

 

<?xml version='1.0' encoding='utf-8' ?>
    <?XML:NAMESPACE PREFIX = "[default] http://schemas.microsoft.com/PowerShell/Snippets" NS = "http://schemas.microsoft.com/PowerShell/Snippets" />
A Template for comment-based help. Expansion
 


    

Was once an enthusiastic PepperByte employee but is now working at Ivanti. His blogs are still valuable to us and we hope to you too.