Whether you are a pro with PowerShell or a beginner, PowerShell ISE snippets can speed up your scripting and assist when you are unsure of formatting.
Avatar

Most IT professionals who work with PowerShell to build tools, scripts, or modules use the built-in PowerShell ISE (Integrated Scripting Environment) to build and debug their code. If you are unfamiliar with the PowerShell ISE console, you should definitely learn more about it here. In summary, the PowerShell ISE is a built-in utility with graphic user interface that supports multiline editing, intellisense, syntax highlighting, and selective execution.

PowerShell Integrated Scripting Environment (ISE)

PowerShell Integrated Scripting Environment (ISE)

The PowerShell ISE has an extremely beneficial feature called snippets. By default, it has several templates that can assist the beginner and the experienced scripter. There are several default snippets available, depending on the version of PowerShell installed. To view the default snippets available to you within the PowerShell ISE console, go to Edit > Start Snippets.

Start Snippets to view the default snippets available to you in the PowerShell ISE console

Start Snippets to view the default snippets available to you in the PowerShell ISE console

If you have noticed, the great thing about this feature is the keyboard shortcut, Ctrl+J. When I am developing or writing PowerShell, the first thing I do when I open my PowerShell ISE is this keyboard shortcut.

I recommend when writing PowerShell functions that you press Ctrl+J and select either Cmdlet (advanced function) or Cmdlet (advanced function) - complete. These two specific snippets allow a user to use a template when creating PowerShell functions in the ISE. Additionally, they have snippets for classes, if statements, for/while/do/do-while/do-until loops, switches, Desired State Configuration (DSC) resources, and many more!

Create an advanced function with a snippet

Create an advanced function with a snippet

Over the years I have formed certain habits that may differ from the built-in snippets. Whether they are good or bad is another discussion. Luckily, the PowerShell team thought about this and created three PowerShell cmdlets:

Built in PowerShell cmdlets for ISE snippets

Built in PowerShell cmdlets for ISE snippets

These three cmdlets allow you to create new snippets, import custom snippets you have created or downloaded, and get snippets currently installed (besides the default snippets).

To create a new snippet, you need to supply the New-IseSnippet cmdlet at minimum a Title, Description, and Template String (Text). Additionally, you can also specify the Author and a Character Offset (Location for the cursor once the template is used).

Here is an example of a new snippet I use when creating a function. This snippet allows me to add a template for a parameter in my function:

$SnippetText = @"
# Param1 help description
[Parameter(Mandatory=$true,
            ValueFromPipelineByPropertyName=$true,
            Position=0)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
$Param1,
"@

$splat = @{
    Title       = 'Basic Function Parameter'
    Description = 'Parameter for a basic function'
    Text        = $SnippetText
    Author      = 'Josh Rickard (MSAdministrator)'
}

New-IseSnippet @splat

When you create the new ISE snippet, it actually creates a new XML file. This XML file has the following filename structure:

{Title you gave your Snippet}.snippets.ps1xml

This file contains the following:

<?xml version='1.0' encoding='utf-8' ?>
    <Snippets  xmlns='http://schemas.microsoft.com/PowerShell/Snippets'>
        <Snippet Version='1.0.0'>
            <Header>
                <Title>Basic Function Parameter</Title>
                <Description>Paramter for a basic function</Description>
                <Author>Josh Rickard (MSAdministrator)</Author>
                <SnippetTypes>
                    <SnippetType>Expansion</SnippetType>
                </SnippetTypes>
            </Header>

            <Code>
                <Script Language='PowerShell' CaretOffset='0'>
                    <![CDATA[# Param1 help description
[Parameter(Mandatory=True, 
            ValueFromPipelineByPropertyName=True, 
            Position=0)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
,]]>
                </Script>
            </Code>

    </Snippet>
</Snippets>

To identify where the new PowerShell ISE snippet is located, we can use the Get-ISESnippet cmdlet. Typically, the output will reside in the user's Documents folder under a folder named Snippets:

Subscribe to 4sysops newsletter!

Snippets folder

Snippets folder

The PowerShell ISE is extremely powerful as it is, but as you gain more experience with PowerShell and the ISE, you will find snippets extremely helpful, and they will speed up your development.

avatar
1 Comment
  1. Avatar

    Very good post Josh!

    Here is one of my most used personal snippet as a System Administrator:

    “`powershell

    $SnippetText = @’
    $ServerList=@(
    ‘Server1’
    ‘Server2’
    )
    foreach($Server in $ServerList)
    {

    }
    ‘@

    $NewSnippetParams = @{
    Title = ‘Foreach with server list from array’
    Description = ‘Foreach function using an array of servers’
    Text = $SnippetText
    CaretOffset = 17
    Author = ‘Luc FULLENWARTH’
    }

    New-IseSnippet @NewSnippetParams

    “`

     

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account