PowerGUI: Save Time, Customize Snippets

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%PowerGUISnippets (Windows 32-bit) or %PROGRAMFILES(x86)%PowerGUISnippets (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 CodeSnippetsCodeSnippetHeader 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 CodeSnippetsCodeSnippetSnippetCode 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!