PowerShell Snippets for Visual Studio Code

If you are a fan of using Visual Studio Code as a lightweight text editor and are also a PowerShell scripter, you have probably found VS Code’s PowerShell support somewhat lacking.  For instance, while it can syntax colorize PowerShell script such as showing $foo in the color for variables, it doesn’t know that ${global:foo} is also a variable.  I have submitted an issue on this along with an improved powershellDef.js file.  You should be able to pull that down from here.  Then copy that into your C:\Program Files (x86)\Microsoft VS Code\resources\app\plugins\vs.language.powershell\out directory.

Another issue is that there are no snippets for PowerShell.  Well, now there is.  Grab this powershell.json snippets file from here and put it in your ~\AppData\Roaming\Code\User\Snippets directory.  This snippets file provides the following snippets:

  • Class
  • Condition double quoted string statement
  • Condition expression statement
  • Condition single quoted string statement
  • Condition statement
  • Do…until loop
  • Do…while loop
  • Else statement
  • ElseIf statement
  • Enum
  • For loop
  • Foreach statement
  • Function
  • Function advanced
  • Help
  • If statement
  • Method
  • Property
  • Property hidden
  • Reverse for loop
  • Switch statement
  • Try catch
  • Try catch finally
  • Try finallly
  • While loop

Once you installed the snippets file, you can create a PowerShell script file – save it as a .PS1 file and Code will recognize it as a PowerShell script.  If it is not saved, press Ctrl+Shift+P to bring up the command palette, select “Change Language Mode” then select PowerShell.  Now type “tr” and you should see a completion list like this appear:


Note that the first entry in the completion list above, with the icon “abc”, is there because the word “try” was used elsewhere in my script file.  This is very basic form of Intellisense providing auto-completion of words that it has encountered in the file. You will want to press the down arrow to select the other entries.  Notice that snippets have an icon that looks like an empty box with dotted lines on the bottom.  And snippets typically display a description as well e.g. “Try catch”.  Press either {Tab} or {Enter} to use the snippet.

Enjoy and let me know if you find any issues.  Note that snippets in Visual Studio Code act a bit weird when auto-completion takes place.  It will no longer allow you to tab through the various placeholders in a snippet.  I’ve submitted that as an issue to the VS Code team.  Hopefully they can fix it (or educate me on how it supposed to work).

This entry was posted in PowerShell, VSCode. Bookmark the permalink.

13 Responses to PowerShell Snippets for Visual Studio Code

  1. Dmitry Koldunov says:

    Good job, Keith!

  2. Josh Duffney says:

    In case you don’t feel like downloading the file and moving it with the mouse.

    function Get-VScodeSnippets () {
    $snippets = (Invoke-WebRequest -URI “https://gist.githubusercontent.com/rkeithhill/60eaccf1676cf08dfb6f/raw/23d6437455cd9adeb64684d37461313f016bb0c6/powershell.json” -UseBasicParsing).Content
    New-Item -Name ‘powershell.json’ -Path “$env:APPDATA\Code\User\snippets” -Value $snippets -Force

  3. How do you switch back to using the PowerShell.json snippets file that comes with the VSCode PowerShell extension instead of the one that you told us how to copy into our Roaming profiles back in 2015?


  4. Pingback: Visual Studio Code Snippets – JKavPowerShell

  5. Steven Spierenburg says:

    I must say, unless I miss something these snippets still have value. vscode does offer some snippets out-of-the-box but you need to ‘construct’ a Function (for example) with several little snippets whereas your snippet “funcadv” will generate the complete structure in 1 go.
    I do advice to remove the comment section as vscode is very good nowadays to create the comment/help structure by typing “<#" just about your function name. Paramaters will be picked up and added to this structure, nice.

    So for funcadv alone I am still using this to this day 🙂

    So thanks Keith!

  6. Pingback: Distraction for Today – Snippets for VSCode – JKavPowerShell

  7. Pingback: Tip: In VS Code type 'ex-cmdlet' [Enter] to get comment header and function syntax - How to Code .NET

  8. Jim says:

    In the advanced function snippet, the $ on the Mandatory=$true statement needs to be escaped “\\”, for me the dollar sign was not making it into the source after choosing the snippet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s