Most administrators use the PowerShell ISE (Integrated Scripting Environment) to build their PowerShell scripts. But by now Microsoft has almost stopped developing PowerShell ISE and recommends using a more powerful, convenient, flexible and free tool instead — Visual Studio Code (VS Code). In this post, we’ll cover how to install, configure and use Visual Studio Code instead of PowerShell ISE to run your PowerShell commands as well as to develop and test complex PowerShell scripts.
VS Code is a cross-platform development environment with a lot of extensions you can use to create a code almost on any programming language. VS Code has the integrated Git support and a vast number of features to work with your code and debug it.
You can download VSCode for free following this link: https://code.visualstudio.com/. Download the VSCodeSetup-x64 installation file (about 53MB) and run it.
VSCode installation does not cause any problems. However, it is recommended to add paths to Visual Studio Code to the environment variable PATH during the installation.
After starting Visual Studio Code, you will have to install a special free extension that supports PowerShell language — ms-vscode.PowerShell (https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell).
This extension gives you advanced options to work with your PowerShell code: syntax-highlighting, snippets, automatic command hinting (IntelliSense), integrated help and cmdlet browser, interactive script debugging, etc.
You can install the extension from the Extension menu in the left sidebar. Search by powershell
key and install the PowerShell extension: Develop PowerShell scripts in Visual Studio Code.
If you are working in an isolated environment, you can install the extension from a VSIX file. Download ms-vscode.PowerShell-2020.3.0.vsix following the link above and install it using the Install from VSIX menu option.
To make it more convenient, I have configured the following VSCode interface settings (a gear icon in the lower left corner).
workbench.colorTheme = PowerShell ISE
– the color scheme is almost alike the familiar one in PowerShell ISEeditor.formatOnType = On
editor.formatOnPaste = On
powershell.integratedConsole.focusConsoleOnExecute = Off
window.zoomLevel = 0
editor.mouseWheelZoom = On
You can also set VSCode settings via JSON file. To do it, click Open Settings (JSON), then copy and paste these settings as plain text here:
{ "workbench.colorTheme": "PowerShell ISE", "editor.formatOnType": true, "editor.formatOnPaste": true, "powershell.integratedConsole.focusConsoleOnExecute": false, "editor.mouseWheelZoom": true, "files.defaultLanguage": "powershell", "editor.fontSize": 12, "terminal.integrated.fontSize": 12, "files.encoding": "utf8" }
VSCode supports a lot of shell and programming languages. To switch between them, press F1. In the bar that appears type Change Language mode and select which language syntax you want to use. Select PowerShell and the icon of the file opened in the active tab will change to the PS one.
Let’s try to use VSCode features to run a PowerShell command and debug scripts.
Create a new project file (it is a common text file). You can work with multiple files simultaneously, they are shown as separate tabs as well and you can switch between them.
Type a simple command to display the list of running processes: Get-Process. As you can see, the IntelliSense technology supports automatic hinting of the command you choose by pressing Tab and the built-in help on the available cmdlet parameters.
In order to execute a single PowerShell command, select the line you need and press F8. If you want to run multiple lines of your PowerShell code, select them with your mouse in the editor window and press F8. You will see the results in the Terminal window. To run the whole PS1 script file, select Terminal -> Run Active File.
In the Terminal window, you can also run PowerShell and cmd.exe commands in the interactive mode.
If you are using functions in your PowerShell code, you will go to the function code when you right-click its name and select Go to Definition.
Using the integrated script debugger (Ctrl+Shift+D) you can see the variable values, reference values and set break points when running your PowerShell script.
I hope that this article will become a good place to start learning and using rich features of Visual Studio Code to develop your PowerShell scripts.