when working your working in a application for a prolonged amount of time your always going to end up repeating the same thing time and time again, when coding in SSMS or the Powershell ISE, we can skip a keystrokes with some handy snippets.

In a previous post i wrote about forgetting the syntax to import the SQLPS module and keep the current location (code below)

IF (!(Get-Module -Name sqlps))
    {
        Write-Host 'Loading SQLPS Module' -ForegroundColor DarkYellow
        Push-Location
        Import-Module sqlps -DisableNameChecking
        Pop-Location 
    }

A better way to keep track of this type of thing is to save it to a snippet in the PowerShell ISE.

To use a snippet you simply go Edit -> Start Snippets or use the short cut Ctrl+J

To create your own you use the New-IseSnippet command. below is my first snippet which turns the above code into a snippet

New-IseSnippet -Title "Import SQLPS" -Description "Imports SQLPS module if not loaded and retains current location" -Text "IF (!(Get-Module -Name sqlps))
    {
        Write-Host 'Loading SQLPS Module' -ForegroundColor DarkYellow
        Push-Location
        Import-Module sqlps -DisableNameChecking
        Pop-Location 
    }"

From there its a simple case of of Ctrl+J and im bingo was namo!

New-SnippetInUse

For more information on how New-Snippet works and what it does in the background you can read more from:
New-IseSnippet Syntax
Snippets in Windows PowerShell ISE 3.0 by the PowerShell Team

Advertisements