Skip to main content

Adding Some Code Templates to the PowerShell ISE

Today I’m sitting at the car dealership waiting on my wife’s car to get a recall fixed.  Luckily I can carry my entire office on my laptop (I’ve been here for 3 hours).  I’m working on finalizing my code for the 2014 PowerShell Summit in April so of course I keep finding better and better ways of writing my own code.  (BTW, my session at the summit is on Tuesday)  What I am focused on is to take my HelpDesk module code to the next level, but also to be able to do it quickly and easily.  This has finally given me a reason to use the Snippets functionality in the PowerShell ISE.

I wrote up some code that will help me a lot and saved it as a PowerShell script called HDSnippet.ps1.  This code will be my template.

Now, before we begin, you need to be on PowerShell V3.  I know that a lot of you who are going to be taking my PowerShell classes over then next few months are still in a V2 environment.  That is OK.  Copy and Paste still work.  For those of you on PowerShell V3, open the ISE and press Ctrl-J.  We are going to be adding to this list.

I’m going to do this as a User-Defined snippet. My default folder for my snippets is C:\Users\JASON\Documents\WindowsPowerShell\Snippets.  Since the Snippets folder does not yet exist, you will want to create it.  This is also where the cmdlet New-IseSnippet will automatically place your new snippet file.

I’m now going to create a little script to help reduce my typing.

$Text = (Get-Content -Raw -Path "C:\Users\JASON\Documents\WIndowsPowerShell\Snippets\HDSnippet.ps1")

 

$Props = @{

    "Title" = "NewHDCmdlet";

    "Description" = "Default code for a new HD module cmdlet";

    "Author" = "Jason A. Yoder"

    "Text" = $Text

 

}

 

New-IseSnippet @Props

 

Since my Snippet is actually code, I have to use the –Raw switch with the Get-Content cmdlet so the Text property of New-IseSnippet processes it correctly.

This will create a new file in the Snippets folder called NewHDCmdlet.snippets.ps1xml.  If you press Ctrl-J now, you will have access to your snippet.  If you decided that you no longer need one of your templates, just delete it from the Snippets folder.

If you want to stop displaying the default snippets and see just yours, in the ISE click Tools –> Options.  Click the General Settings tab and uncheck the Use default snippets.

image

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.