Creating VSCode User Snippets for PowerShell

If you spend time working with VSCode you’ll learn there are cool functions like “User Snippets” you can customize. I’ve talked about these before on another blog and showcased creating User Snippets that allow you to import the Configuration Manager PowerShell module. I wanted to go a step further with a few more snippets I’ve written.

The PowerShell parameter block

If you have the PowerShell extension for VSCode installed something you might do occasionally is create a parameter. When you start to type the word “parameter” you get an auto complete option like this.

While pretty cool it requires me to go back and change a few more things than I want. Including important items like the parameter type and the parameter name.

Creating a user snippet

Creating a user snippet is quick and easy in VSCode. The UserSnippet function uses JSON code to define what goes where and how. Each snippet needs four features.

  • Snippet name
  • Prefix
  • description
  • Body

The snippet name must be a unique name for the snippet in the definition file. The Prefix is what is used to call the code snippet via IntelliSense. The description is the help message displayed when IntelliSense loads the command. Finally the “Body” is the actual code block that is created.

When you create a user snippet use good naming practices and follow the proper JSON formatting. It’s also worth noting that VSCode supports the variables for dynamic information in snippets. You can read more about that below.

https://code.visualstudio.com/docs/editor/userdefinedsnippets

Parameter Snippets

The below code block is an example of a “User Snippet” which creatse a new parameter of type “string” with other information. This allows me to remove the comments I don’t want and pre-stage others. I could add other things including “mandatory”,”isnotnullorempty” etc. but this is a generalization.

	"StringParamater" : {
		"prefix": "newparam",
		"description": "Creates a parameter for strings",
		"body": [
			"[Parameter(HelpMessage = )]",
			"[string]$ParameterName",
		],
	},

We don’t need to stop with just a string parameter we can take this a step further what if we want an integer, a boolean or a switch parameter? We can do those just as easily.

"IntParameter" : {
		"prefix": "newparam",
		"description": "Creates a parameter for Integers",
		"body": [
			"[Parameter(HelpMessage = )]",
			"[Int32]$ParameterName",
		],
	},

	"BoolParameter" : {
		"prefix": "newparam",
		"description": "Creates a parameter for booleans",
		"body": [
			"[Parameter(HelpMessage = )]",
			"[bool]$ParameterName",
		],
	},

	"SwitchParamater" : {
		"prefix": "newparam",
		"description": "Creates a parameter for switches",
		"body": [
			"[Parameter(HelpMessage = )]",
			"[switch]$ParameterName",
		],
	}

Notice I’ve opted to use the SAME prefix for all these options. This is for a specific reason. When you use the same prefix in VSCode it brings up all the options together as a group when you start to type the prefix in. For example:

This may seem stupid but it gives me a QUICK way to build out certain parameters I use frequently.

I’ll update this as blog post or create a new one whenever I create a new set of VSCode Snippets. Eventually I plan to have a copy of my PowerShell.JSON available for download on GitHub.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: