Getting Started with Visual Studio Code for Use with PowerShell

Now that I’ve hopefully piqued your interest in using Visual Studio Code for editing and debugging PowerShell scripts, here is how you get started.  First, go to the Visual Studio code web site to download and install VSCode.  From here you can click on the Download for Windows button to install VSCode on your Windows machine:

image

Note: if you are on Linux or Mac OS, scroll to the bottom of this page to download the respective versions.

Once you have installed VSCode, install the PowerShell Editor Services extension by pressing Ctrl+P, then type “ext install PowerShell” and install the extension named “PowerShell” by clicking on the button to the right of the “README” button:

image

Now try opening a directory that contains some of your PowerShell scripts.  Open the File menu and select “Open Folder …”.  Then select the folder containing your scripts.  Once you do this, all of the scripts will show up in the Explore tab of the Side Bar:

image

As you select different files (or use the cursor keys and enter key) they will show up in the editor window on the right.  If you want to put a file in your “Working Files” list, double-click it (or drag it to the Working Files section).

You can have up to three editor panes open at once.  You can open a new editor window by pressing Ctrl+\.  You can switch between the three editor windows using Ctrl+1, Ctrl+2 and Ctrl+3.   Also, when you have this many editor windows open you may want to hide the Side Bar which you can do with Ctrl+B (toggles the Side Bar on and off):

image

Also, you can use various commands from the command palette for things like searching for a file in the folder you have open. Type Ctrl+P and start typing part of the filename to narrow the search down.  Note that VSCode does a fuzzy search so the part of the name you remember can appear anywhere in the filename e.g.:

image

Finally, for this segment, I want to point out the PowerShell editor services configuration settings.  Unless you are developing against the extension itself there isn’t much for you at the moment but you may be interested in one of the settings that turns script analysis on and off.

To edit your user settings, press Ctrl+Shift+P, then type “user” and press enter.  Your editor should look like this to start with (unless you’ve already configured user settings) and you will need to scroll down in the left editor window to get to the powershell section:

image

The editor window on the left is the Default Settings for VSCode and all installed extensions.  Note that the default for “powershell.scriptAnalysis.enable” is “true”.  To change this setting, copy the line from the left to your user settings file in the right editor pane between the curly braces.  Try this trick, place the cursor in the word “true” on the line you just copied to your settings.json file and press Ctrl+Shift+<period>.  Notice how the true changes to false?  Pretty slick.

It is likely this will be the only setting for the PowerShell extension you will want to change.  Be sure to save the changes to your settings.json file.

I recommend that you look at the rest of the settings in the Default Settings file to see which other settings you’d like to configure in your settings.json file.  One I like to override is:

//-------- Files configuration --------
"files.trimTrailingWhitespace": true,

Next up, we will take a look at how to configure VSCode for debugging PowerShell scripts.

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

7 Responses to Getting Started with Visual Studio Code for Use with PowerShell

  1. Pingback: Debugging PowerShell Script with Visual Studio Code | Keith Hill's Blog

  2. Pingback: New Release of PowerShell Extension For Visual Studio Code | Keith Hill's Blog

  3. Pingback: Preview of Module Build and Publish with Visual Studio Code and the PowerShell Extension | Keith Hill's Blog

  4. Pingback: Make Visual Studio Code more like the integrated scripting environment - Windows Wide Open

  5. Charlie Spencer says:

    Okay, I’m grass-green when it comes to developer tools, or development for that matter. I also realize the post I’m replying to is a couple of years old, but I’m grabbing at straws.

    I’m trying Visual Studio Code as an alternative to PowerShell ISE. The application opens okay but as soon as I open a .PS1 file, I get an error ‘The language service could not be started’. Googling the error wasn’t any help.

    Any suggestions on where I might find a solution? PS 5, W10 64 Ent, VSC 1.14.2
    Thanks, regardless.

    • rkeithhill says:

      It looks like you found the right forum to ask that question – on the GitHub issues for the PowerShell extension.

      • Charlie Spencer says:

        Yeah, somehow I tripped over it. I don’t remember how I found it; I was trying so many things. it definitely wasn’t by Googling ‘visual studio code powershell language service’.

        Thanks for the reply.

Leave a reply to rkeithhill Cancel reply