Shortly before MMS this week I was starting to work on some PowerShell where I was constantly creating and adding parameters and I realized that the default parameters in VSCode weren’t quite what I wanted them to be so I decided to dig into how I could update them and customize them to really do what I want. I had done some stuff with User Snippets before but I wanted to make some that would actually be useful to me. So I started digging into it.

If you’ve ever worked with VScode you should know that basically anything can be tweaked and changed as an auto build item using JSON. OK but what does that mean for us. I figured we would start with something that was pretty popular earlier this year and that was importing the Configuration Manager module. If you write lots of tiny scripts for Configuration Manager you have to remember how to import the Configuration Module if you’ve read my earlier blog on how to import the configuration manager module this takes that to the next level. Doing this is pretty easy, first we need to know the code that we will use to import the ConfigMgr module.

(Join-Path $(Split-Path $ENV:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)

Our second pre-requisite is that you need to have the PowerShell extension installed on VSCode. If you have not installed this please do so it’s very easy on the left hand pane select “Extensions” and then search for the PowerShell extension from the Gallery.

OK that’s simple enough next we need to crack open VSCode and navigate to “File -> Preferences -> User Snippets

This will then open the VSCode command window in the center bar from here you will want to type in “PowerShell” this will cause it to search for the PowerShell JSON Library that is stored on your machine. Now since I’ve opened mine it’s already in the search bar but if you haven’t it will likely be further down the list. Opening this will open the PowerShell snippets for you.

 

Once you open this field you’ll be able to start editing the JSON file. When the JSON file opens by default there isn’t really anything in it. You’ll need to start adding some data into the form but first you need to know what the heck the data does. When you open this you’ll get the following “help” information explaining what the JSON does in a comment block.

// Place your snippets for powershell here. Each snippet is defined under a snippet name and has a prefix, body and 
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
// same ids are connected.
// Example:
// "Print to console": {
// 	"prefix": "log",
// 	"body": [
// 		"console.log('$1');",
// 		"$2"
// 	],
// 	"description": "Log output to console"
// }

Let’s break down what’s happening here.

Print to Console – this is the friendly name of the snippet this MUST be a unique when it is entered so you can tell your snippets apart. I’ll use this typically as one or two words.

 

Prefix – This is the short name that you will type into your VSCode editor. You can use the same prefix for multiple code snippits. This is useful when you have a set of code snips that are all similar in nature but have little nuances that you want to apply.

 

Body – The body is the code that you want to actually be used. Important things of note because it’s Java its REALLY sensitive to formatting. Most basic commands will just work if you put them in there with double quotes and a comma at the end of each line. If you want white space you must use empty quotes.

 

Description – This is the explanation that is displayed by the intelisense in the editor. Use complete and short descriptions of what your quick snippet does.

With all of that in mind here is an example of how we could create a snippet for importing the configuration  manager module – and first check to see if the admin console is installed before trying to import it.

"Import Configuration Manager Module" : {
  "prefix": "Import-CM-Module",
  "body": [
    "if($$ENV:SMS_ADMIN_UI_PATH)",
    "{",
    "(Join-Path $(Split-Path $$ENV:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)",
    "}",
  ],
  "description": "This Snippet is used to import the Configuration Manager PowerShell module in scripts that are run on a machine where the console is installed."
},

Now if I was to put Import-CM-Module in my VSCode editor I would get the following output

 

Once you’ve done this a simple tab will finish auto-filling.

 

 

In closing here are a few more useful snippets that I am currently using. I will also add more in the future to my problem resolution repository.

"StringParamater" : {
  "prefix": "newparam",
  "body": [
    "[Parameter(HelpMessage = )]",
    "[string]$ParameterName",
  ],
  "description": "This is used to create a standard String Paramater in the format I most commonly use."
},
    
"IntParameter" : {
  "prefix": "newparam",
  "body": [
    "[Parameter(HelpMessage = )]",
    "[Int32]$ParameterName",
  ],
  "description": "This is used to create a standard Integer Paramater in the format I most commonly use."
},
    
"BoolParameter" : {
  "prefix": "newparam",
  "body": [
    "[Parameter(HelpMessage = )]",
    "[bool]$ParameterName",
  ],
  "description": "This is used to create a standard boolean Paramater in the format I most commonly use."
},
    
"SwitchParamater" : {
  "prefix": "newparam",
  "body": [
    "[Parameter(HelpMessage = )]",
    "[switch]$ParameterName",
  ],
  "description": "This is used to create a standard Switch Paramater in the format I most commonly use."
},

The above items will allow you to automatically generate a relatively clean parameter block when writing scripts and functions as the default parameter includes a bunch of “fluff” that needs to be edited and removed.

 

I hope you find this helpful and if you want more tips and tricks about PowerShell and VSCode please let me know on Twitter or in the comments down below.

 

(3457)

comments
  • Tim
    Posted at 21:18 May 7, 2019
    Tim
    Reply
    Author

    This is awesome! It looks like in your example, the “$” $ENV: is being ignored by VSCode because it sees it as a placeholder. If you double “$$” in the it will retain the $ENV:

    “Import Configuration Manager Module” : {
    “prefix”: “Import-CM-Module”,
    “body”: [
    “if($$ENV:SMS_ADMIN_UI_PATH)”,
    “{“,
    “(Join-Path $(Split-Path $$ENV:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)”,
    “}”,
    ],
    “description”: “This Snippet is used to import the Configuration Manager PowerShell module in scripts that are run on a machine where the console is installed.”
    },
    }

    • Jordan Benzing
      Posted at 17:07 May 8, 2019
      Jordan Benzing
      Reply
      Author

      You are 100% correct I was half asleep when I was writing this and missed the escape character. Thanks for the spot – Fixed in the code.

  • Jordan H
    Posted at 16:25 May 8, 2019
    Jordan H
    Reply
    Author

    I would love more tips and tricks about PowerShell and VSCode please. I’ve lightly used Powershell ISE but mainly Notepad++ and Powershell. Now that I’m learning GitHub and Git, VSCode has been great.

    • Jordan Benzing
      Posted at 17:07 May 8, 2019
      Jordan Benzing
      Reply
      Author

      Sounds great I’ll see what I can do 🙂

  • Leave a Reply to Tim
    Cancel Reply

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