Default Powershell Terminal is 7.2 but scripts run as Powershell 5.1

5.1k views Asked by At

Notice in my VS Code, a fresh terminal, the very first line indicates it is properly defaulted to Powershell 7.2.2 - just as I believe it should.

But then in this same screenshot, the first script I run (which has a command to print out the powershell version) it shows the PSVersion is 5.1.19041.1320

vs code default terminal screenshot

Here's all my powershell specific VSCode entries from settings.json

    "terminal.integrated.profiles.windows": {
        "MyPowerShell_7": {
          "path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
          "args": ["-NoProfile"]
        }
    },
    "terminal.integrated.defaultProfile.windows": "MyPowerShell_7",
    "powershell.powerShellAdditionalExePaths": [
        {
            "exePath": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
            "versionName": "MyPowerShell_7"
        }
    ],
    "powershell.powerShellDefaultVersion": "MyPowerShell_7",

Why why why is it so hard to run the Test-Json cmdlet?

2

There are 2 answers

0
bkwdesign On BEST ANSWER

finally found an answer here on reddit

I didn't know that 5.1 and 7 are side-by-side and have different ways to invoke each application

So, if I change my command from powershell to pwsh I get the right result

enter image description here

0
Filip De Vos On

Actually, when you have a PowerShell or pwsh terminal open (Which you have here) you should call your script directly and it will be executed in that host.

So instead of

PS C:\>pwsh -file './scripts/ps/experiment.ps1'` 

you should call

PS C:\>./scripts/ps/experiment.ps1

The way you are calling your scripts will launch a new host, run the script and then terminate.