1

I have a strange problem with Visual Studio code related to selecting the default Powershell version: I have installed Powershell 5.1 and 7 parallely, but when I doubleclick on a ps1-file, it should automatically run the Powershell 5 console and execute the script.

This works flawlessly on my computer. However, as soon as I right-click a ps1-file and select "Open with" -> "VSCode", the file is opened in VS Code. When I now close VSCode and doubleclick the ps1-File in Windows Explorer again, the file will be opened in VSCode instead of Powershell 5 console, which is not my expected behaviour.

I believe that VSCode somehow changes the default handler for ps1-files from 5 to 7 in some way.

I have already tried to select the Windows powerShell as the default powerShell Extension in VSCode (by Clicking the plus button in the Terminal Window, Select Default Profile, and select Windows Powershell). This change seems to be completely ignored in VSCode, because I see from the console output that it always wants to open the ps1 file using PowerShell 7, instead of 5.

Do you have any idea how to get rid of this unexpected behaviour? Thank you!

rioV8
  • 24,506
  • 3
  • 32
  • 49
Erik
  • 2,316
  • 9
  • 36
  • 58

1 Answers1

0

To fix the problem interactively:

  • In File Explorer, right-click on a .ps1 file and select Open with > Choose another app.

  • In the dialog that opens, click on Choose an app on your PC

  • Navigate to powershell.exe or paste its full path, typically C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe and submit.

  • Click on Always

This persistently causes .ps1 files to be opened with powershell.exe, though the automatically created command line needs tweaking for full robustness / additional customizations, such as wanting to keep the resulting PowerShell session alive and the window open:

  • Run regedit.exe and navigate to HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command

  • Edit that key's default value by placing -File before "%1" to ensure that script files whose paths contain spaces are properly opened.

  • Optionally, if you want to keep the session and window open after the script finishes executing, place -NoExit before -File

mklement0
  • 382,024
  • 64
  • 607
  • 775