Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select which version of PowerShell (5 or 7) is used when I run PowerShell code in VS Code?

I am using Windows 11, which comes with Windows PowerShell version 5.x preinstalled. I also installed PowerShell version 7.x and VS Code. In VS Code, I want to choose whether my code runs in Windows PowerShell or PowerShell 7.

I tried changing the default terminal profile values, but without success. Regardless of the terminal I choose, my code always runs in PowerShell version 7.x.

Changing the default profile of the integrated terminal in Windows

Here you can see that I have only one Windows PowerShell terminal open. Running $PSVersionTable in this terminal reveals that it is version 5.

PSv5 terminal

When I hit the run button in a Windows PowerShell v5 terminal, hit the run button in a v5 terminal

a new PowerShell version 7 terminal gets created, and the code returns version 7.

new v7 terminal

How can I choose which PowerShell version is used when hitting run?

The only way I know is to first open a terminal of my choice and then run the PowerShell script (.ps1 file) in that terminal window. Is there a better way to choose the PowerShell version when hitting run in VS Code?

Running a ps1 in v5 terminal

like image 424
Situ Avatar asked Dec 31 '25 06:12

Situ


1 Answers

Your screenshot shows two distinct types of shells being run, governed by distinct configurations:

  • The one labeled powershell is a general-purpose shell.

  • The one labeled PowerShell Extension is a special-purpose shell, the so-called PIC (PowerShell-Integrated Console) provided by the PowerShell extension, which is the one you're interested in.

To control what PowerShell executable (and therefore what edition and version) the PIC uses, as Santiago points out, you can press F1 and search for PowerShell: Session Menu, then choose the appropriate Switch to: command to configure a different PowerShell executable to use in the PIC.

For a general overview of how to configure the various shells used in Visual Studio Code, including how to add PowerShell executables in nonstandard locations to the PowerShell extension's session menu, see this answer.

like image 84
mklement0 Avatar answered Jan 03 '26 14:01

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!