For bigger PowerShell-projects containing custom functions and modules, user defined snippets can help to boost productivity when writing specific scripts. This post outlines how to start creating and using these from within VS Code.

Setting the scene

I created a big PowerShell library to support things like pre-provisioning of OneDrives, setting permissions, requesting team creation via an API. The project source code is located in DevOps and VS Code is used to maintain and for further development.

The project contains a set of inhouse built PowerShell modules with standard functions to accomplish the things mentioned above. Within the VS Code workspace, the functions are used to create tailormade PowerShell scripts for specific scenarios.

A lot of the customers projects have common scripts which are slightly adjusted to accommodate a specific requirement like the way authentication is done. So each time a script is created, the use of each function needs to be typed in. This can be done quicker through the use of snippets.

In the old days, you could use the New-IseSnippet cmdlet in PowerShell ISE to create snippets. But VS Code makes this way easier to create, maintain and use.

Creating the snippets

To get started with snippets for the scope of your project, you can create a file in the ‘.vscode’ folder of your project with the filename ‘{name}.code-snippets’. This file can contain a set of snippets each containing a title with child-elements ‘prefix’, ‘body’ and ‘description’ like in the following example:

	"Connect SPO": {
		"prefix": "_Factory",
		"body": "\\$connection = ConnectPnPOnline -Url \\$url -Interactive -ReturnConnection",
		"description": "Connect to a SharePoint Online site interactively."
	"Get sourceItems": {
		"prefix": "_Factory",
		"body": "\\$sourceItems = Get-FactorySourceItems -sourcePath $sourcePath",
		"description": "Get the source items."
	"Import table": {
		"prefix": "_Factory",
		"body": "\\$table = Import-Csv -Path \\$inputfile -delimiter $delimiter",
		"description": "Import a CSV table."

Please notice the escape-characters used for the variables in each snippet.

Using the snippets

After creating a couple of snippets, you can use the prefix (‘_Factory’ in the example) to show a selection list in VS Code.

Selecting Code Snippets

So by starting to type the prefix, the available snippets are shown in a list to choose and pick. When actually selecting one of the snippets, VS Code perfectly handles the escapes for the variables.

Use Code Snippet

The result, the expected line of code which can be fit into the script and tailored for the scenario at hand.

I’ve started using this a lot, and it does indeed save time opposed to writing the lines yourself. The snippets file is saved with the project (in DevOps/Github), so easily to maintain during the lifespan of the project.

For more information, see Snippets in Visual Studio Code, as there’s much more power under the hood to explore!