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!
01/21/2013 at 4:01 pm
Brilliant. Thank you!