Here is the quick and dirty way to add multiple PowerShell versions to VSCode, and switch between them quickly.
It’s often times advantageous to quickly switch between multiple versions of a programming language when coding to ensure that your code works on multiple platforms. For me, that is switching between Windows PowerShell and multiple versions of Microsoft PowerShell (PowerShell Core). He’s how to easily do it in VSCode.
First, I am going to assume you have the PowerShell extension already installed in VSCode. If not, hit F1 (Ctrl-Shift-P) to open the command palette, and type this
ext install powershell
Now, let’s get some different PowerShell versions. This is the link to PowerShell 7 preview 5. Down that and extract it to any directory you want. In this example I will also be using the latest stable version – 6.2.3. In this example I have downloaded them both to the c:\pwsh directory.
Open VSCode, and create a new file. Save the file with a .ps1 extension. Notice how the terminal automatically starts PowerShell, and that the little green number in the lower right says 5.1? That’s because VSCode is smart enough to look in common locations for PowerShell versions, and will add those automatically to your session options. Since we are putting the version in a non-typical location, we have to edit our settings.json. The easiest way to do it is this is to hit F1 (Ctrl-Shift-P), and type ‘language’. Select “Configure Language Specific Settings”
You will open the settings.json for your user and language. If you haven’t done much with VSCode, it probably looks something like this:
{
"workbench.iconTheme": "vscode-icons",
"team.showWelcomeMessage": false,
"markdown.extension.toc.githubCompatibility": true,
"git.enableSmartCommit": true,
"git.autofetch": true,
"[powershell]": {}
}
To add our new versions, we need to add just a bit to this file. We are going to add powershell.powerShellAdditionalExePaths settings – in our case 2 of them. The important parts are to make sure you are escaping the “\” slashes with another slash, and just follow normal JSON formatting requirements. Continuing with my example, my settings.json looks like this:
{
"workbench.iconTheme": "vscode-icons",
"team.showWelcomeMessage": false,
"markdown.extension.toc.githubCompatibility": true,
"git.enableSmartCommit": true,
"git.autofetch": true,
"[powershell]": {},
"powershell.powerShellAdditionalExePaths": [
{
"exePath": "C:\\pwsh\\7.p5\\pwsh.exe",
"versionName": "PowerShell Core 7p5"
},
{
"exePath": "C:\\pwsh\\6.2.3\\pwsh.exe",
"versionName": "PowerShell Core 6.2.3"
}
],
"powershell.powerShellDefaultVersion": "PowerShell Core 7p5",
"powershell.powerShellExePath": "C:\\pwsh\\7.p5\\pwsh.exe"
}
Save the settings file, and if you have an open terminal, either exit it, or crash/reload. This will force VSCode to reload the settings file. If you have done it correctly, if you click on the little green version number in the lower right, you should see something like this:
Switch between them, and verify you see the various version with $PSVersionTable