Using VSCode snippets to apply your script template

Previously I wrote about the need for standardizing scripts and showed the template that I made. The question is how to easily start your scripts from the template.  You can open the template and save it as a new file but I think that is extra work I do not want to do.  If your script editor allows for the use of code snippets, things become easier.  My editor of choice is Visual Studio Code (VSCode) and I will show you how to create a code snippet for your template.  You can use this to easily add repeatable code to your scripts.  Again, this is being done with VSCode which is what I recommend as I love the script analyzer that it has as well as the ability to expand its functionality with community created extensions.

We need to access the User Snippets in VSCode by going to PreferencesUser Snippets under File.

There are two options.  You can use a language specific snippets file that is already there or create a new global snippet file.  I prefer to create me own global snippet file because it is easier for me to copy to another location for backing it up.

Select New Global Snippet file and give your file a name and location to save it.  I leave it in the default location.

Once you click ok.  You are presented with the file in VSCode which includes a basic explanation on how to format your snippet as well as an example.  Add your snippet or as many as you need after the last commented out line and above the closing }.

The basic information you need is what you want to name the snippet.  I am calling mine Template.  You need to set the scope which is what languages you want to snippet to show up for.  More on that in a bit. You set the prefix which is like a short name.  I went with temp.  Then you add the code that the snippet will insert into your script.  Each line of the code needs to sit between “”.

Before I show what my snippet looks like, we need to discuss Scope.  As I said, the scope is what languages you want the snippet to work with.  Before it will show a snippet for the language you are using, you first need to save the script.  Also, what if you have a snippet that you want available regardless of language?  The easiest way to do this is to just omit the scope line as it mentions in the explanation created when you made the global snippet file.  This makes the snippets without a scope available as soon as you start a new script or regardless of script language used.

Here is what my snippet looks like.

"Template": {
"prefix": "temp",
"body": [
"<#",
".SYNOPSIS",
"Script synopsis",
"",
".SYNTAX",
"Scrpt syntax",
"",
".DESCRIPTION",
"Script description",
"",
".PARAMETER ",
"Parameter input descriptions and repeat this as needed for each parameter",
"",
".INPUTS",
"Input description if used",
"",
".OUTPUTS",
"Output description if used",
"",
".NOTES",
"FileName:",
"Author:",
"Contact:",
"Created:",
"Modified:",
"Version:",
"",
".EXAMPLE",
"Example description and example",
"",
"#>",
"",
"#-----Parameters-----",
"",
"Param (",
"",
"",
")",

"#-----Initializations and Module Imports-----",
"",
"#-----Variables-----",
"",
"#-----Functions-----",
"",
"#-----Execution-----"
],
"description": "PowerShell Template"
},

You then just save the file and reload VSCode.

Start a new file and type in the prefix you set and hit tab.

The prefix is replaced with my code snippet.  As you will notice, the text is all white.  That is because the file has not been saved yet.  Once it is save, you code will become color coded.

Now VSCode does have some variables that you can use as well and you can grab environmental variables.  One thing I did was add variables from year, month, and date to auto populate the created field.  The format is YYYYMMDD.

".NOTES",
"FileName: ",
"Author: ",
"Contact: ",
"Created: $CURRENT_YEAR$CURRENT_MONTH$CURRENT_DATE",
"Modified: ",
"Version: ",

You can find more information at https://code.visualstudio.com/docs/editor/userdefinedsnippets.

While this is all done with VSCode, there are other editors that allow you to add code snippets.