<#

Functions are great productivity helpers for frequently used or critical tasks. But for those things that are done only occasionally, it becomes hard to remember that you have a function for that, what the exact command is, what the parameters are, and what values are needed. To address this I’d like to introduce you to the SNIPPET.

Snippets are a feature of Powershell ISE. Powershell ISE is the tool you should be using for administration and it is my recommendation that you always have it up and running during work (time to retire POPS).

A snippet is just like a template (sometimes called a boiler plate). It can contain any sort of text. It can be used for scripts, functions, notes, reminders; generally whatever you’d like to be able to paste into a document. You open the snippet dialog box with CTRL + J (or in the toolbar EDIT > Start Snippets). The collection of snippets is displayed as a list. When you mouse over an item of interest, a window pops up with the text of the snippet. You double-click (or hit enter on the keyboard) to insert the text (your cursor should be at the spot where you want the insert to go). This can be either in the script pane (the white background area) or the command console itself.
Initially when you open the snippet dialog box there will be a list of default snippets. This can be turned off in the toolbar: Tools > Options: General Settings tab – Other Settings/Use default snippets (uncheck). Unfortunately, the option is only to turn them off or on (you cannot select individual ones). I’d leave them on to start with. However, what is important is that you can create your own and add them to the list! There is even a Powershell command to do this (actually it is the only way to do it):

New-IseSnippet [-Title] <String> [-Description] <String> [-Text] <String> [-Author <String>] [-CaretOffset <Int32>] [-Force]

Here is how it works:
New-IseSnippet
–Title (How you want it labeled)
–Description (your description. It shows at the very top so it is best if this is short)
–Text (This is where you are going to put your commands and parameters).

These are the required parts of the command meaning that if you just type New-IseSnippet and hit enter, it will ask you for those three items. The optional parameters are: –Author (author name) and –CaretOffset. CaretOffset allows you to specify where you want the cursor to be after pasting in the command (like right at the part where you put in the first parameter…)

Want a snippet for resetting AD account passwords?
NEW-ISESNIPPET –TITLE “RESET PASSWORD” –DESCRIPTION “FORCES THEM TO CHANGE PASSWORD AT NEXT LOGON” –TEXT ‘$NEWPWD=CONVERTTO-SECURESTRING –STRING “somePassword” -ASPLAINTEXT -FORCE ; SET-ADACCOUNTPASSWORD -IDENTITY -NEWPASSWORD $NEWPWD –RESET -PASSTHRU | SET-ADUSER –CHANGEPASSWORDATLOGON $TRUE’ -CARETOFFSET 108

You run this command in your ISE one-time and the command will be created and added to your snippet list, ready to help the next time someone has forgotten their password. When they call, hit Ctrl+J in the ISE window, find “Reset Password” entry, and double-click it (or hit the enter key). The command string will be inserted with the cursor at the place where the logon name is inputted. (In this case, either username or FQDN will work).

Once you create a snippet, it is stored as a XML type file (<SnippetTitle>.ps1xml) in your profile (documents/WindowsPowershell/Snippets) directory. This means it is only available for you. But you can share this file with anyone. They would just put a copy in their profile directory location. This is also how you remove any practice or unwanted snippets (delete/remove them from this folder).

Having one-liners available this quick is really essential to being efficient with Powershell. Practice this one until you are comfortable making your own snippets. Like using Get-Help or Get-Command, this is an essential skill in using Powershell.

tags: Powershell, Snippets, Productivity, Oneliners, Powershell ISE, ISE

#>

Advertisements

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