The Black Cloud

PowerShell VSCode snippets

Continuation of the first VSCode settings post if you will 😊

Given that I have worked with many engineers over the years, I have noticed that very few of them are aware of the snippet feature in VSCode. Even fewer actually use them.

So maybe this will help somebody out there 😊

VSCode PowerShell snippets

So what are PowerShell snippets and why should you care?

Snippets are pre-defined code templates that you can insert into your code to save time and reduce errors. They are a great way to speed up your workflow and improve your code quality.

My favourite snippet is the calculated-property snippet, which creates a PowerShell calculated property with the proper syntax.

How often do you want to get what I would call a custom property that you want to select and in line do something with. Like this:

1
Select-Object -Property *, @{Name='CustomProperty';Expression={$_.'SomeProperty'}}

I for one, never remember the syntax for that, so this snippet saves me a lot of time 😊

The easiest example is Get-Disk with some calculated properties:

1
Get-Disk | Select-Object -Property FriendlyName, @{ Name='SizeGB'; Expression = { [Math]::Round($_.Size / 1GB, 2) } }, @{ Name='SizeTB'; Expression = { [Math]::Round($_.Size / 1TB, 2) } }

Example output

1
2
3
FriendlyName        SizeGB SizeTB
------------        ------ ------
VMware Virtual disk 200.00   0.20

How to insert a snippet

To insert a snippet, press Ctrl + Alt + J to bring up the menu, type the snippet name and press Enter.

Inserting a VSCode snippet

Where are the VSCode PowerShell snippets located?

NOTE
Most likely there will not be a powershell.json file there, so you will need to create it.
1
"C:\Users\<Username>\AppData\Roaming\Code\User\snippets\powershell.json"

If you want to use the $env:APPDATA variable:

1
"$($env:APPDATA)\Code\User\snippets\powershell.json"

Sample snippet file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{
	"CredentialObject": {
		"prefix": [
			"credential",
			"pscredential",
			"credentialobject",
			"newcredential"
		],
		"body": [
			"\\$Username = \"$1\"",
			"\\$Password = ConvertTo-SecureString -String $0 -AsPlainText -Force",
			"# Create credential object",
			"\\$Creds = New-Object -TypeName PSCredential -ArgumentList \\$Username, \\$Password"
		],
		"description": "Create a PowerShell PSCredential object."
	}
}

This creates a snippet that can be used in PowerShell files with the prefix credential, pscredential, credentialobject, or newcredential. It looks like this:

1
2
3
4
$Username = ""
$Password = ConvertTo-SecureString -String  -AsPlainText -Force
# Create credential object
$Creds = New-Object -TypeName PSCredential -ArgumentList $Username, $Password

Super handy if you ask me 🤣

JSON escape characters tips for the snippet file

NOTE

JSON escape characters:

  • For a tab: \t
  • To escape literal $ (dollar sign) prefix the dollar sign with: \\
  • To escape double quotes prefix with: \
  • For a blank space use: ""

Bonus tip

The best thing about those snippets is that if you synchronise your VSCode settings using Settings Sync, those snippets will be synchronised too! But maybe that is a blog post for another day 😄

<< Previous Post

|

Next Post >>

#Blog #PowerShell