- OpenVPN IPv6 and IPv4 configuration - Mon, Mar 1 2021
- 4sysops author and member competition 2020 - Fri, Jan 1 2021
- Assign an IPv6 address to an EC2 instance (dual stack) - Tue, Dec 15 2020
In my previous post, I explained how you can add PowerShell Core 6.0 as a terminal to VSCode and work with multiple different shells side by side. We have seen that VSCode comes with basic support for PowerShell and offers features such as syntax highlighting.
VSCode PowerShell extension ^
However, to get a similar convenient feature in PowerShell ISE, you need to install the Visual Studio Code PowerShell extension. When you first save a file with the suffix .ps1, VSCode will recommend installing the PowerShell extension. You can then simply click the Install button.
The PowerShell extension comes with its own terminal (the PowerShell Integrated Console), which you can use in addition to the shells we configured in my last post. Unfortunately, the PowerShell Integrated Console does not support syntax highlighting like the other shells we've used before.
You can run selected code in the editor by pressing F8. If you want to execute a particular line, you only have to move the cursor to that position. This is useful, but better ways exist to run code snippets in VSCode which I will cover below.
IntelliSense shows you your options as you type, and when you hover with the mouse over a cmdlet, you'll see its syntax.
Auto-completion allows you to complete the command or parameter by pressing the Tab key (press it multiple times to get all the options). This also works in the console.
PowerShell Script Analyzer checks the quality of your code. You might want to configure PSScriptAnalyzer because its default rules are perhaps a bit too strict if you only want to execute code snippets on your console.
To do so, press CTRL+SHIFT+P and start typing "scriptan…" Then select PSScriptAnalyzer Rules and press Enter. You can then select and deselect rules.
The extension has more useful features for scripting in PowerShell, which you can find on the extension's page and in the FAQ. You can also press CTRL+SHIFT+P and then type "PowerShell" to discover more features. Here, we focus on using VSCode as a console and don't cover all those features.
Running selected code from the editor ^
The main reason I like to use a scripting environment as a console is you can type your command in the editor and run it from there. This is useful if you run the same or a similar command many times a day or if you are unsure about the correct syntax and have to edit your command several times until it finally works. Sometimes I have multiple versions of the command in the editor and just have to select the one that fits best for the current task. You can also write a quick little script if a one-liner is not going to do the job.
Above, I mentioned that the PowerShell extension allows you to execute code from the editor by pressing F8. If you want to execute an entire script, you first have to select the complete code, which is not what we know from ISE. Moreover, the PowerShell extension always executes code in its own terminal, which does not support syntax highlighting, and it uses Windows PowerShell 5.1 instead of PowerShell Core 6.0.
Unfortunately, the built-in way to run code from the VSCode editor is also a bit longwinded. But no worries—we will fix that in a moment. Let's see first how the code-execution feature built into VSCode works.
Open a new file first (CTRL+N), and save it (CTRL+S). It is important you've added the extension .ps1 because otherwise VSCode won't recognize that your code is PowerShell. Once you've saved the file, syntax highlighting will work.
Now select one of the PowerShell terminals and then mark the code you want to execute in the editor. Next, press Ctrl+Shift+P and type "run sel." Then chose Terminal: Run Selected Text In Active Terminal.
This will be faster if you assign a shortcut to the Run Selected Text command (File > Preferences).
Executing Code with Code Runner ^
However, with the help of the Code Runner extension, it is more convenient to execute code from the editor. To install the extension, click the Extensions icon on the left and type "code runner." Install the extension and then click Reload.
Before I explain how to use Code Runner, we have to change its configuration a little. By default, Code Runner will execute code in the Output tab. This is not good if you want to run an interactive script or if you want to continue working in the console after you've launched the command.
To tell Code Runner to run code in the Terminal, you have to add the line below to your VSCode settings. Make sure you separate all your settings with commas as shown in the screenshot below.
By default, Code Runner will execute PowerShell code with Windows PowerShell on a Windows machine. To tell Code Runner to use PowerShell Core, you can add the following lines to your settings.json:
"powershell": "pwsh -ExecutionPolicy ByPass -File",
You can see we also can configure the execution policy here. Your configuration should look like the screenshot below.
Code Runner offers several ways to run code from the editor. As in PowerShell ISE, you can execute the entire script by simply clicking the Run Code icon or pressing CTRL+ALT+N. Note that Code Runner will run the version of the script you've last saved and not the current code in the editor window. Also note that Code Runner uses its own terminal and not the one you opened before.
If you only want to run selected code, you can right-click and click Run Code in the context menu, or you can press CTRL+ALT+N after you select the code. To stop running the code, you can press CTRL+ALT+M.
Code Runner offers a variety of settings I don't cover in this blog post. You can find them all in the package.json file in "$env:UserProfile\.vscode\extensions\formulahendry.code-runner-0.8.7" on a Windows machine. On Linux and macOS, you can find the extensions under ~/.vscode/extensions. To change one of these settings, you don't have edit package.json. Just copy the settings to your VSCode user settings and change the configuration there.
The downside of Code Runner is that it can only execute commands in its own terminal. Thus, you have to decide if you want to work with Windows PowerShell or PowerShell Core. The PowerShell extension has the same restrictions and its console does not offer syntax highlighting. The built-in code execution feature of VSCode has the advantage that it executes the code in the active terminal, but you always have to first select the code.
That is why I usually work with Code Runner and only use the built-in feature if I have to execute code in a shell other than my default shell. Of course, this doesn't mean that you don't need the PowerShell extension. Its features are very useful in the editor.
Subscribe to 4sysops newsletter!
Even though I've only scratched the surface of VSCode in my two articles, I think you've noticed that this scripting environment is much more powerful than PowerShell ISE. Thus, I hope I've convinced you to move on and leave ISE behind. I also encourage you to work with PowerShell Core 6 instead of Windows PowerShell. Visual Studio Code is the perfect environment to run both PowerShell versions side by side for a while.