Using Central ISE Snippet Repository

by May 7, 2013

In a previous tip we illustrated that ISE code snippets (press CTRL+J to view them) are plain ps1xml-files that you can manage in File Explorer. By default, the PowerShell 3.0 ISE editor loads snippets from your private cache folder.

Imagine you and your colleagues set up a network drive to share useful snippets. Instead of manually checking this folder and manually copying the snippets from this folder onto your local machine, you can also tell the ISE editor to load the snippets directly from the central repository. Here is a sample:

$snippetPath = Join-Path (Split-Path $profile.CurrentUserCurrentHost) "Snippets"
$snippetPath

Get-ChildItem -Path $snippetPath -Filter *.ps1xml | 
  ForEach-Object {
    $psise.CurrentPowerShellTab.Snippets.Load($_.FullName)
  }

This sample loads the snippets from your private local folder, so it shows what ISE does by default. Simply assign another path to $snippetPath to load the snippets from another folder. This could be your central team repository, and it could also be a USB stick you are carrying around with you that holds your favorite snippets.

As it turns out, this is the code executed behind the scenes by Import-IseSnippet. In fact, all *-IseSnippet commands are functions, and you can view the source code like this:

$path = "$pshome\modules\ise\ise.psm1"
ise $path

Twitter This Tip! ReTweet this Tip!