Installing and using Visual Studio Code

This article discusses how to install and use the Visual Studio (VS) Code development environment with the 1E PowerShell Toolkit. VS Code provides a far more feature-rich development and debugging environment than the PowerShell Integrated Scripting Environment (ISE). When you have completed the process described below, you will be able to author new PowerShell scripts using VS Code and also debug these scripts or the 1E PowerShell Toolkit itself.

Installing VS Code

  1. Download VS Code. It is recommended that you select the 64-bit system installer, which will make VS Code available to all users on the machine. When the installation is complete, you should have the option to run VS Code checked. Click Finish. VS Code should launch automatically.

  2. Open the 1E PowerShell Toolkit folder. Select the File/Open folder and browse to and select the folder in which the Toolkit files are saved.

  3. Open the Terminal window by selecting View/Terminal. You may need to press Enter for a prompt to appear.

    By default, VS Code will attempt to run PowerShell Core (the .NET core version of PowerShell). If this cannot be located, it will roll back to standard PowerShell.

  4. Import the 1E PowerShell Toolkit module. In the Terminal window, type the following:

    Copy
    import-module .\ps1Etoolkit.psd1

    To confirm that the module is loaded, type the following. This should return the current toolkit version.

    Copy
    get-1Etoolkitversion
  5. In the Explorer pane, click the ps1Etoolkit.psm1 file.

  6. On the bottom right of the VS Code screen, you should now have a prompt to install the recommended extensions for PowerShell. Select Install. You may receive an additional prompt that a new version of PackageManagement should be installed. Select Yes to install this. Some additional prompts may appear in the Terminal window. Select Y or click to continue.

  7. To ensure everything is correctly initialized, exit and then re-run VS Code. Your previous files are reloaded automatically.

  8. Repeat the commands in the Import the 1E PowerShell Toolkit module step above to reload the Toolkit.

Start debugging

The PS1EToolkit.psm1 file contents should still be visible on the first code tab. Select Run/Start debugging from the VSCode menu. You can ignore the error Cannot run a document in the middle of a pipeline.

Setting a breakpoint 

  1. Press ctrl+F and then type in get-toolkit. You should locate the code which contains the Get-1EToolkitVersion cmdlet.

    The cmdlet is called Get-ToolkitVersion in the code. This is because the module manifest file (PS1EToolkit.psd1) automatically adds a namespace prefix (1E) to all cmdlets. This is so that the cmdlets do not conflict with any existing PowerShell cmdlets. However, the code itself does not contain this prefix.

  2. Scroll down to the line that reads the following:

    Copy
    return $script:TOOLKITVERSION
  3. Select Run/Toggle Breakpoint to set a breakpoint on this line.

  4. Run the following command:

    Copy
    get-1Etoolkitversion

    The breakpoint should be highlighted. If this does not occur, and you see an error dialogue on the bottom right, select Yes in the dialog to continue. Then exit and re-enter VS Code and reload the Toolkit (as described in the Import the 1E PowerShell Toolkit module step in Installing VS Code above), and then select Run/Start debugging. Then re-enter the get-1Etoolkitversion cmdlet.

  5. When the breakpoint is highlighted, press F5 to continue. The code should continue and the toolkit version will be displayed in the Terminal window.

    You now have VS Code configured for developing and debugging PowerShell.

Customization

Early versions of VS Code lacked a decent UI for customization, requiring some JSON tweaks. This is now much improved, with most obvious customization options being available via the File/Preferences menu.

Among others, you can change the theme, you can use a feature called minimap, which shows a reduced version of the whole code base on the right-hand side of the screen, and you can also specify if you want to have the last active folder automatically reloaded each time you start VS Code.

User installs of VS Code

It's possible to create a user install of VS Code that can be copied to a portable storage device such as a USB thumb drive, making it possible to take a configured copy of VS Code and simply copy it onto a target machine, as appropriate.