Add Code Snippets to Your Script by Using the PowerShell ISE

ScriptingGuy1

Summary: Learn how to modify the Windows PowerShell ISE to automatically add code snippets.

 

Microsoft Scripting Guy Ed Wilson here. There are advantages and disadvantages to using multiple computers. One of the advantages is that I get to see where I have assumed a dependency on some particular thing that I did not know existed. Another advantage is that I get to remember how to do things that I did a long time ago when setting up a particular computer. For example, I am sure that you have a particular way that you like to view files and folders in Windows Explorer. If that particular view is different from the default behavior of Windows Explorer, every time you install a new copy of Windows, you get to go through the interface and make the same tweaks and changes. A better way to do that is to write a script to make those changes for you, but of course, that does not include the tweak you made while reading some blog.

I am using a different computer today than the one I normally use when writing this blog, and as I started going through my Windows PowerShell ISE module and the Add-MenuItems.ps1 script, I realized there were more dependencies than I wanted it to have. If one reads all of the Hey, Scripting Guy! Blog posts, and follows along with all of the code samples, things are pretty simple. If on the other hand that is not the case, I discovered it is not such a trivial manner to get the ISE module set up and working the way it currently exists on my other computer. Hmm, that sounds like a project that I will soon tackle: creating an installation routine for the current Windows PowerShell ISE module. The really cool thing is that we will soon have the ability to upload and download files from the Scripting Guys Script Repository, and that will vastly simplify my ability to share files with you.

Anyway, today I want to be able to easily add code snippets to a script from inside the Windows PowerShell ISE. This is the fourth article in the series.

Here is a review of where we currently stand. First I wrote a script that uses a graphical file dialog box to browse for .ps1 and .psm1 file types. When I select a file from the dialog box, the content of the file is added automatically to the current insertion point of the script. Next, I wrote a script that creates a custom .snip file extension and associates it with Notepad. Yesterday, I talked about code snippets, and wrote a script that downloaded a snip.zip file from the Scripting Guys Sky Drive, created a snippet folder under the Windows PowerShell folder in your user profile, and expanded the snip.zip file to copy all of the 11 .snip files to the snippet folder. I then modified the graphical file dialog box to include browsing for .snip file types.

The first thing I do is copy the Get-CodeSnippetV2.ps1 script to the snippet folder that was created yesterday when I installed the .snip files. That folder is shown in the following image.

Image of snippet folder

One thing that is a bit “interesting” is that in Windows Explorer, the folder appears as “My Documents,” but from inside Windows PowerShell, it appears as “Documents.” I imagine that is done for clarity, and is not to be confused with the link that says “My Documents” that always produces an “Access denied” error because of the null ACL.

Anyway, by putting everything in a single folder, it makes cleanup easier and has the added advantage of ensuring user rights to the folder, because one always has access to one’s own profile (even if it is a bit confusing to access; the solution is to add a variable that points to the location).

After I have copied the Get-CodeSnippetV2.ps1 script to my snippet folder, I open the script and comment out the demo line I added yesterday that called the Get-CodeSnippetV2 function. The reason for this is that I will dot source the script in my Windows PowerShell ISE profile, and I do not want the function to run when it is loaded. The line that I comment out is shown here. This line of code was the last line in the script.

# Get-CodeSnippetV2 -InitialDirectory (Join-path -path (Split-Path -path $profile -Parent) -ChildPath snippet)

Now that I have commented out the line of code that calls the function, I create a new variable called snipHome. This variable refers to the folder where the code snippets were installed yesterday. The command to create the new variable and dot source the Get-CodeSnippetV2 function appear here.

New-Variable -Name snipHome -Value `

(Join-path -path (Split-Path -path $profile -Parent) -ChildPath snippet)

. $snipHome\Get-CodeSnippetV2.ps1

My revised Windows PowerShell ISE profile appears here in its entirety.

# *** Entry Point to Profile ***

Import-Module ISEProfileModule

backUp-Profile

New-Variable -Name snipHome -Value `

(Join-path -path (Split-Path -path $profile -Parent) -ChildPath snippet)

. $snipHome\Get-CodeSnippetV2.ps1

. $moduleHome\iseProfileModule\add-MenuItems.ps1 | out-null

My Windows PowerShell ISE profile appears in the Windows PowerShell ISE as shown in the following image.

Image of Ed's Windows PowerShell ISE profile

As you can see, the last command in my Windows PowerShell ISE profile is the command that dot sources the Add-MenuItems.ps1 script. The Add-MenuItems.ps1 script is the script that creates the Add-Ons menu on the Windows PowerShell ISE. It makes sense to add the Get-CodeSnippetV2 function to the menu. I decided to create two new menu items. One will allow me to browse code snippets, and the other will add the code snippets to the current insertion point in a new script.

The code that appears here is the code I add to allow me to either browse or add code snippets.

$CodeSnippets = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(

“CodeSnippets”,$null,$null)

$CodeSnippets.Submenus.Add(“BrowseSnippet”,

{Get-CodeSnippetV2 -initialDirectory $snipHome}, “Ctrl+Alt+B”)

$CodeSnippets.SubMenus.Add(“AddSnippet”,

{Get-CodeSnippetV2 -initialDirectory $snipHome -add}, “Ctrl+Alt+A”)

The Add-MenuItems.ps1 script was developed in the Adding Custom Menu Items to the Windows PowerShell ISE. It has also been referenced in several other Scripting Guy posts. The completely revised Add-MenuItems.ps1 script appears here.

Add-MenuItems.ps1

$RestoreRoot = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(

“Restore”,$null,$null)

$RestoreRoot.Submenus.Add(“RestoreALLDefaults”,

{$psISE.Options.RestoreDefaults()}, “Ctrl+Alt+R”)

$RestoreRoot.SubMenus.Add(“RestoreTokenColorDefaults”,

{$psISE.Options.RestoreDefaultTokenColors()}, “Ctrl+Alt+T”)

$GetOptions = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(

“GetOptions”,$null,$null)

$GetOptions.SubMenus.Add(“GetFonts”, { C:\fso\Get-PsISEfonts.ps1 } , $null)

$GetOptions.SubMenus.Add(“GetColors”, { C:\fso\Get-PsIseColorValues.ps1 } , $null)

$CodeSnippets = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(

“CodeSnippets”,$null,$null)

$CodeSnippets.Submenus.Add(“BrowseSnippet”,

{Get-CodeSnippetV2 -initialDirectory $snipHome}, “Ctrl+Alt+B”)

$CodeSnippets.SubMenus.Add(“AddSnippet”,

{Get-CodeSnippetV2 -initialDirectory $snipHome -add}, “Ctrl+Alt+A”)

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(

“MyCustomISE”, { C:\fso\Set-PsISEcolorsAndFonts.ps1 },$null)

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(“ClearMenu”,

{ $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Clear() }, $null)

When I press Ctrl+Alt+B, the browse snippet window appears, as shown in the following image.

Image of snippet folder

To add a snippet, I position the insertion point at the place in my script that I need the snippet, and I press Ctrl+Alt+A, and then I press the first letter of the snippet. The options appear in the File name box, and I can use the down arrow to select the snippet I need, and press Enter. The process goes rather fast and is quicker than typing the code structures.

The following image illustrates inserting several code structures using the add code snippet command.

Image of inserting code structures by using add code snippet command

 

Well, this is about all there is to working with custom code snippets. I will clean all this up, and provide a couple of revised scripts to do a clean installation of everything. For now, I am going to head outside to my woodworking shop and enjoy the rest of the weekend.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon