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.

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

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

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

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?

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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With