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.

Josh Rickard

Josh's primary focus is in Windows security and PowerShell automation. He is a GIAC Certified Windows Security Administrator (GCWN) and GIAC Certified Forensic Analyst (GCFA). You can reach Josh at MSAdministrator.com or on Twitter at @MS_dministrator.

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:

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:

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:

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.

Win the monthly 4sysops member prize for IT pros

Share
2+

Users who have LIKED this post:

  • avatar

Related Posts

1 Comment
  1. Luc Fullenwarth 3 weeks ago

    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

    ```

     

    1+

Leave a reply

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

*

CONTACT US

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

Sending
© 4sysops 2006 - 2018

Log in with your credentials

or    

Forgot your details?

Create Account