A Snippet to Create New Snippets in PowerShell ISE v3

12/01/2012

PowerShell

I came across this article on how to create a new snippet in PowerShell ISE v3. I wanted to take it a step further and have a snippet for creating new snippets so not to have to remember this each time.

Here is the code to create a new snippet:

[scriptblock]$code = 
{#PutYourCodeHere
}
 
New-IseSnippet `
    -Text $code.ToString() `
    -CaretOffset $code.ToString().Length `
    -Force `
    -Title ‘SnippetTitle’ `
    -Description ‘SnippetDescription’

To create a snippet for creating new snippets, we simply replace #PutYourCodeHere with the complete code above and then run it to load the new snippet. Your result should look like this:

[scriptblock]$code = 
{
    [scriptblock]$code = 
    {#PutYourCodeHere
    }
 
    New-IseSnippet `
        -Text $code.ToString() `
        -CaretOffset $code.ToString().Length `
        -Force `
        -Title ‘SnippetTitle’ `
        -Description ‘SnippetDescription’
}
 
New-IseSnippet `
    -Text $code.ToString() `
    -CaretOffset $code.ToString().Length `
    -Force `
    -Title ‘New-Snippet’ `
    -Description ‘Use to create a new snippet’

You can now call the snippet but simply pressing CTRL+J and then typing New-Snippet and ENTER:

You just need to now replace #PutYourCodeHere, the Title and Description and you are done!

Advertisements
,

Subscribe

Subscribe to our RSS feed and social profiles to receive updates.

One Comment on “A Snippet to Create New Snippets in PowerShell ISE v3”

  1. Christopher Rhodes Says:

    Brilliant. Thank you!

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: