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

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.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the ma...