31 Days of Our Favorite Things: Getting Snippy with PowerShell 3.0 in Windows Server 2012 (Part 11 of 31)

pshell3As most of you know I am a huge fan of PowerShell and as I took you on a brief glimpse of PowerShell 3.0 on part 3.  There is even so much more to share.  Often times when I am working with folks new to PowerShell I spend a lot of time working on proper syntax and getting the basics down.  Just like football, learning the blocking and tackling of PowerShell can carry you a long way.  The product group thinks so too, that is why they have put in some fantastic tools to help us learn more about PowerShell 3.0, like what I discussed in part 3 the show=command pane and IntelliSense.  Well in today’s post we are going to take a look at another great addition called Integrated Script Snippets.

Integrated Script Snippets

With all of the new functionality inside PowerShell 3.0 including improved sessions, PowerShell workflows, job scheduling, …etc, the product provided the snippets to help.  The Integrated Script Snippets are stored in the Integrated  Scripting Environment (ISE) and are designed to help us learn to PowerShell as well as write proper scripts.  When you access the snippets you can select from a list of script templates, select the appropriate template, and have partially completed script inserted into the editor. By default ISE ships with several script snippets to ease creating the commonly used programming syntax patterns.  Here are the default built-in snippets:

  • Cmdlet (advanced function)
  • Cmdlet (advanced function) – complete
  • Comment block
  • do-until
  • do-while
  • for
  • foreach
  • function
  • if
  • if-else
  • switch
  • try-catch-finally
  • try-finally
  • while
  • Workflow (advanced)
  • Worflow (simple)
  • Worflow ForEachParallel
  • Workflow InlineScript
  • Workflow Parallel
  • Workflow Sequence

 

Working with Snippets

The snippets are very easy to work with.To access Integrated Script Snippets, in the ISE simply press the keystroke (Ctrl-J), alternatively you can access the snippets via the edit menu by selecting Start Snippets and you will see a screen similar to this one:

image.

To select a snippet click it and you will see a description, to insert the snippet either double click or hit enter.  The snippet will be dropped in either the scripting pane or the scThe following is an example of Worflow (simple) snippet:

image

 

Last word on Snippets

One last note on snippets.  The Built-in snippets are just one of three possible snippets available to you in the PowerShell 3.0 ISE.   You can find snippets in modules and even create them yourself (for user-defined snippets).  Some modules you may load in the future may have snippets in them.  You can easily import them with the Import-IseSnippet cmdlet, however at the writing of this post I could not find any other snippets in the built-in roles in Windows Server 2012, I am sure I missed checking all of them, but no luck.  Let me know if you find any more.  To learn more about user defined snippets check the link below to the PowerShell teams blog.  Hopefully you have began down the path learning PowerShell 3.0.  If you have not here is a kick start for you from the series I did in 2011: PowerShell Not your Father’s Command Line 31 Days of PowerShell.  The series will get you started, however PowerShell 3.0 is gotten even bigger. PowerShell 3.0 now provides a comprehensive platform to help you manage server roles and automate management tasks where you can accomplish virtually anything now with PowerShell.  Even better now is no better time to learn PowerShell, it is now easier learn and implement than ever before. 

To learn more about Snippets and how to create your own user defined snippets take a look at this article by the PowerShell team: Snippets in Windows PowerShell ISE 3.0 

We hope you are enjoying the series and check out the PowerShell 3.0 by taking a look at these downloads:

If you missed any of the parts you can find the rest of the series here: 31 Days of Our Favorite Things in Windows Server 2012.