less than 1 minute read

Just a quick post, as much as a reminder for me as anything, but also useful to those that attended my sessions last week where I talked about snippets in PowerShell ISE

Jeff Hicks wrote a post explaining how to create snippets in VS Code for PowerShell

I love using snippets so I went and converted my snippets list for ISE (available on GitHub) into the json required for VS Code (available on GitHub)

Here is an example of snippet

"SMO-Server": {  
        "prefix": "SMO-Server",  
        "body": [  
            "$$srv = New-Object Microsoft.SqlServer.Management.Smo.Server $$Server"  
        ],  
        "description": "Creates a SQL Server SMO Object"  
    },

I followed this process in this order

Click File –> Preferences –> User Snippets and type PowerShell or edit $env:\appdata\code\user\snippets\powershell.json

In order I converted the code in the existing snippets “Text” like this

  1. Replace `$ with $$
  2. Replace \ with \
  3. Replace ” with \”
  4. \r for new line
  5. \t for tab
  6. Each line in “”
  7. , at the end of each line in the body   except the last one

Look out for red or green squiggles 🙂

  • I then add
  • The name of the snippet, first before the : in “”
  • The prefix is what you type to get the snippet
  • The body is the code following the above Find and Replaces
  • The description is the description!!

and save and I have snippets in VS Code 🙂

snippets.gif

That should help you to convert existing ISE snippets into VS Code PowerShell snippets and save you time and keystrokes 🙂

Comments