Trevor Sullivan's Tech Room

Minding the gap between administration and development

PowerGUI: Save Time, Customize Snippets

Posted by Trevor Sullivan on 2010/10/01


Introduction

Quest’s PowerGUI Integrated Development Environment (IDE) offers the ability to insert code snippets, similar to Microsoft Visual Studio. This saves a lot of typing, and gives you the general framework for whichever PowerShell construct you’re trying to us, for which you only need to fill in the specifics. These code snippets are conveniently stored as files with the .snippet extension. They contain XML that describes the code snippets, and allow you to fill in certain key fields with the IDE.

Snippet Screenshot

The snippet files are stored in a “Snippets” folder beneath the folder where you installed PowerGUI. This is typically %PROGRAMFILES%\PowerGUI\Snippets (Windows 32-bit) or %PROGRAMFILES(x86)%\PowerGUI\Snippets (Windows 64-bit). Because these are simply XML documents—which are nothing more than plain text, really—you can open them in an editor such as Notepad++ or Microsoft’s XML Notepad 2007.

Customizing Snippets

Code snippets can be customized by making edits to the various fields, or code, and saving the file. You’ll need to launch your text or XML editor with your administrative credentials (if you have User Account Control (UAC) on) in order to modify these files, because they are write-restricted from standard users. Once you’ve opened a snippet file, you’ll see a couple of different sections that you can customize. The \CodeSnippets\CodeSnippet\Header section contains some metadata about the code snippet, including: it’s title, the author, a brief description, and the shortcut you can use in the IDE to reference it (rather than digging through the snippet list). If you check out the \CodeSnippets\CodeSnippet\Snippet\Code section, you’ll find the actual PowerShell code that’s embedded in your script when you select the code snippet.

image

By making some simple edits to this section, you can tweak the code that’s inserted into your script from this snippet.

One example of an edit I made, is to comment out the New-Alias, and subsequent Export-ModuleMember section from this snippet: function (module, public, advanced).snippet. Since I’m not generally interested in creating aliases for my advanced functions, I don’t need these lines. I may want them later on though, so rather than remove them, I simply comment them out in the .snippet file.

Conclusion

This article has been a brief demonstration of PowerShell code snippets in Quest’s PowerGUI Script Editor. You can also create your own snippets by copying the structure of any of the code snippets included with the latest version of the software. If you do create any code snippets that you think would be helpful to the community, please be sure to visit http://powergui.org and share your code snippets with the PowerShell community.

As an aside, if you haven’t already gotten involved, there is a great community on the PowerGUI discussion forums! If you are having trouble with a script, or simply want to help out by answering someone else’s question, community participation is always encouraged and welcomed.

Happy PowerShell Scripting!

Advertisements

6 Responses to “PowerGUI: Save Time, Customize Snippets”

  1. Robert Robelo said

    Good tip. The PowerGUI team certainly have a better vision than the PSTeam had for their ISE. Granted, one can add this functionality to the ISE, but it would be much better, and safer, if there was a standard built-in code snippets feature.

    • Trevor Sullivan said

      I tend to agree — I think Microsoft did the typical “here’s an obligatory tool” and left it up to ISVs to write more in-depth ones. Have you looked at Idera PowerShell Plus? I’ve only briefly touched it, but it looks really cool — I keep wondering if I should purchase a copy. $150 is a bit pricey for me personally though, so I’m happy to use PowerGUI for the time being.

      • Robert Robelo said

        Yes, PowerShell Plus is excellent but too expensive. I tried it during its incipient stage but stopped because they did not add, or permit, Windows accesibilty features (Ease of access) such as Sticky Keys. I submitted the suggestion but it never got through, I guess.

  2. Dmitry Sotnikov said

    On thing I would add to this is that if for some reason you don’t like editing XML directly, there are some free snippet editors to help you out. See my blog post on PowerShell code snippets in PowerGUI.

    Dmitry

  3. […] http://trevorsullivan.net/2010/10/01/powergui-save-time-customize-snippets/ Esta entrada fue publicada en Sullivan Trevor. Guarda el enlace […]

  4. Dmitry Sotnikov said

    And now there is also an add-on for PowerGUI Script Editor providing GUI for snippet management: http://dmitrysotnikov.wordpress.com/2010/11/12/snippet-manager-in-powergui-script-editor/

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: