Setup VS Code for PowerShell

I will admit I was resistant to using Visual Studio Code for PowerShell and remained attached to PowerShell Studio and PowerShell ISE until only recently; However, I was watching a video from a PowerShell conference where the presenter was using VS Code and the benefits of VS Code rushed in to focus all at once. After that video I took some time to setup VS Code properly and test out some of the features and now it’s my go to editor for PowerShell.

What is VS Code

Visual Studio Code is a light weight code editor developed by Microsoft that works cross-platform on Windows, Linux, and Mac OS X. It supports multiple languages, has theme and extension support, supports code formatting and highlighting, and is highly customizable. Best of all? It’s free.

It really seems that Microsoft’s direction for PowerShell is to push users towards VS Code. The consistent experience when running PowerShell Core on a system other than Windows likely being the primary reason. I doubt ISE is going anywhere for the time being but it’s obvious a lot of energy has been redirected towards the PowerShell ability of VS Code.

Benefits for PowerShell

The benefits of various editors are entirely subjective (Vim vs Emacs vs Nano anyone?) but the following short list contains the features that convinced me to switch.

In no particular order:

  • VS Code has better and customizable code formatting.
    VS Code can either format on paste, format while you type, format on save, and/or format when you run the formatter manually. There are also a number of settings for things like bracket placement, situations that allow for breaking the rules, automatic whitespace, tab size, and so on. Formatting options are also per-language allowing you to set options for PowerShell separate from similarly structured languages.
  • VS Code has support for other languages besides PowerShell.
    This not only makes it a one-stop application for all of your coding needs, it also means all of the different file types in large PowerShell projects are handled correctly. From your markdown files to your ps1xml files VS Code applies the correct formatting and highlighting.
  • VS Code’s workspace workflow makes it easier to handle multiple files.
    When you open a folder in VS Code all of the files and nested folders are visible in the sidebar making them easy to navigate and open and close as they’re needed. You can also easily create new files, rename files, or delete files right from the workspace browser. For multi-file projects like modules the overview of the whole project simplifies things greatly. You can even add folders from other locations to an existing workspace such as a dependent module’s source folder.
  • VS Code is endlessly customizable.
    Whether themes, extensions, or the almost excessive number of settings if there’s something you want to change you probably can. It is also possible to save workspace specific settings so when you share a project with a team everyone’s VS Code behaves the same way where it counts (tab size, bracket placement, etc.)
  • VS Code has a tasks that you can define to run all sorts of jobs.
    It’s possible in VS Code to launch things like a psake build step using keyboard shortcuts. Tasks are defined in a json file that can be stored in the workspace and can do anything your command-line can do. https://code.visualstudio.com/docs/editor/tasks
  • VS Code has built in support for Git.
    All of your Git commands can be done using the VS Code interface. You can do you commits, pushes, diffs, etc all from VS Code. You will need to install Git on your system before you can use the features but once installed it will automatically detect the .git directory in your workspaces and start working.
    vscode-git
  • When installed, VS Code will use PSScriptAnalyzer as an in-line linter.
    A linter is a tool that  analyzes source code to flag errors, stylistic problems, etc. VS Code will use PSScriptAnalyzer to highlight any rule violations as you code. If you workspace contains a script analyzer settings file you can also define the  "powershell.scriptAnalysis.settingsPath" setting to use your custom rules and share them with your team. https://github.com/PowerShell/PSScriptAnalyzer

Some other minor things:

  • The markdown preview lets you run a side-by-side preview window as your write markdown in VS Code to see how it will look.
    vscode-markdown-preview
  • VS Code ships with Emmet support. Emmet is a tool for very quickly writing html, css, xml, etc and can make writing ps1xml files a cinch with custom snippets.
  • In PowerShell when you run  code . from the command-line VS Code will open a workspace at the current directory.
  • You can define your own snippets easily and even include them in your project so other people working on it have access to the same snippets.

Still Needs Improvement

There are a few things that are still not the greatest in VS Code but for me they’re not enough to stop me using it.

  • Tab completion doesn’t work for ValidateSet parameters. For example, in ISE if you type  -ErrorAction s and then press tab it will complete “SilentlyContinue,” VS Code will not. It will only complete the cmdlets and the parameters themselves. There are some other examples of odd tab completion behaviour that is apparently being worked on. https://github.com/PowerShell/vscode-powershell/issues/25
    • Tab complete only works once IntelliSense kicks in which can be delayed.
    • Tab complete doesn’t cycle through choice. Once it completes that’s it. <- Biggest issue
  • Multi-cursor behaviour in VS Code is weird. When you use multi-cursor in VS Code the only way out of it is to press Esc the mutli-cursor shortcut is also Ctrl+Alt+Shift+↑/↓ instead of Ctrl+Shift+↑/↓ like most editors.
  • A number of people dislike the way clear works in the integrated console. https://github.com/PowerShell/vscode-powershell/issues/551

Setting it Up

Setting Visual Studio Code up for PowerShell is incredibly simple.

  1. Download VS Code from https://code.visualstudio.com and install it. Select the options that make sense to you during setup.
  2. Launch VS Code and head over to the Extensions menu  on the left or press Ctrl+Shift+X on Windows.
  3. Search for PowerShell, select it, and click Install on the right side.
    install-powershell-vscode
  4. Open your settings by clicking the gear in the bottom left and choosing Settings.
  5. Add the following two settings in the left hand panel:
  6. Save your changes by pressing Ctrl+S on your keyboard or by using File -> Save.
    vscode-basic-settings

Congratulations! Visual Studio Code is now setup for PowerShell

My Setup

I have made some additional changes in Visual Studio Code which I will detail my reasoning for and that you may or may not wish to use yourself.

Extensions

I have installed the following extensions (on top of the PowerShell extension:)

  • Code Spell CheckerChecks spelling in function names, variables, and strings. If you create a variable called $Vallues for example it will catch it and highlight it.
  • vscode-iconsProvides better filetype icons on tabs and in the workspace browser.
  • XML ToolsUseful when working with ps1xml files or raw XML.
  • Code Alignment: Adds tasks for lining up statements within code (line up ‘=’  within a hashtable for example.)

Settings

My user settings look like this:

Explanation

  • Basic Settings
    • “workbench.iconTheme”: “vscode-icons” – Changes the icons in the folder/workspace view to the theme vscode-icons (downloaded as an extension.)
    • “workbench.colorTheme”: “Monokai Dimmed” – Personally I like the Monokai theme and I like the dimmed version best.
    • “editor.renderControlCharacters”: true – Shows new-line characters and similar.
    • “files.trimTrailingWhitespace”: true – Removes trailing space when saving files.
    • “editor.minimap.enabled”: false – Turns off the right-hand code map.
    • “editor.fontSize”: 13 – My preferred font size.
  • Emmet
    • Emmet is a very powerful snippet tool for multiple languages include HTML, CSS, and XML. I create my own snippets both for web and for working with ps1xml files so these settings are necessary to make it work.
  • PowerShell
    • “files.defaultLanguage”: “powershell” – Sets the default language for new files to PowerShell.
    • “powershell.scriptAnalysis.enable”: true – Uses PSScriptAnalyzer (installed using Install-Module ) to analyze code as you type.
    • “powershell.scriptAnalysis.settingsPath”: “ScriptAnalyzerSettings.psd1” – Looks for a file called ScriptAnalyzerSettings.psd1 at the root of open workspaces.
    • “powershell.integratedConsole.focusConsoleOnExecute”: false – Stops the console from taking cursor focus when you run a script or selection.
    • “powershell.enableProfileLoading”: true – Loads your PowerShell profiles into VS Code.
    • “powershell.startAutomatically”: true – Ensures the PowerShell extension is loaded when you open PowerShell files.
    • “powershell.codeFormatting.ignoreOneLineBlock”: true – Lets you write short constructs on a single line without them being formatted to multiple lines.
    • “powershell.codeFormatting.openBraceOnSameLine”: true – Keeps the opening brace on the same line as the construct.
    • “files.associations”: { “*.ps1xml”: “xml” } – Tells VS Code to treat ps1xml files like XML.

Leave a Reply

Your email address will not be published. Required fields are marked *