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

  Kiến thức lập trình

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?