Is it possible to use Powershell 7 as the debug console version in Visual Studio Code? [duplicate] - powershell

What is the difference between [pwsh] and [Powershell Integreted Console] on VS Code?
I usualy use pwsh.
Today, when I installed the powershell extension and then tried to update the powershell, the Powershell Integreted Console started up and found out. What is this?  

pwsh[.exe] is the executable file name of PowerShell [Core] (v6+), the cross-platform edition of PowerShell built on .NET Core / .NET 5+; by contrast, powershell.exe is the executable name of the legacy Windows PowerShell edition (v5.1-), built on the Windows-only .NET Framework (v4.8-)
Windows PowerShell comes with Windows itself, whereas PowerShell [Core] must be installed on demand (on all supported platforms).[+]
VSCode (Visual Studio Code) has an integrated terminal (console) that can run any shell, such as cmd.exe, bash, or pwsh or powershell.
Shells that run in the integrated terminal by default have no special integration with the files being edited. They show by their executable file name in the dropdown list in the integrated terminal's toolbar; e.g.:
By contrast, the PowerShell Integrated Console is a special shell that comes with the PowerShell extension and offers integration with PowerShell code being edited, notably to provide linting and debugging support, among other features.
The PowerShell Integrated Console starts on demand when you first open/activate an editor with PowerShell code in a session, and it shows as follows in the integrated terminal's toolbar:
You can use the PowerShell extension's configuration to choose the specific PowerShell executable to use, which on Windows allows you to choose between running PowerShell [Core] (pwsh.exe) and Windows PowerShell (powershell.exe) in the PowerShell Integrated Console.
If a PowerShell [Core] version is installed and it is installed in a well-known location[*], it will be used by default; the fallback on
Windows is Windows PowerShell. Since PowerShell [Core] versions can be installed side by side, you can switch between different versions, if installed.
If a PowerShell [Core] version in installed in a nonstandard location and is therefore not discovered automatically, you can tell the PowerShell extension where to find it, either via the Settings GUI or via settings.json, as shown in the linked topic and the bottom section of this answer.
Whenever the active tab is a PowerShell source-code file, the status bar in the bottom-right corner shows the PowerShell version that is being used if you however over or click on the {} icon; versions >= 6 imply PowerShell [Core].
If configured, there's a separate icon that directly invokes the PowerShell session menu when clicked and which reflects the active version number - again, see the bottom section of this answer; e.g.:
[+] as of v7.0; time will tell if PowerShell [Core] will ship with future versions of Windows, and perhaps even other platforms.
[*] From the linked docs: "This feature looks at a few well-known paths on different operating systems to discover install locations of PowerShell. If you installed PowerShell to a non-typical location, it might not show up initially in the Session Menu. You can extend the session menu by adding your own custom paths." If you use one of the official installers, the PowerShell extension should find your installation.

Related

Running & Debugging PowerShell Script without VSCode PowerShell Extension

Issues
Running PowerShell Script using VSCode PowerShell Extension & running scripts in PowerShell terminal produces different behaviors.
For example,
loading dotnet classes to the current PowerShell session using VSCode PowerShell Extension fails but success with PowerShell terminal.
Using MicrosoftPowerBIMgmt module to perform Get-PowerBIActivityEvent retrieves empty property values for each property, while using PowerShell terminal retrieves full objects from Power BI Service.
Therefore, I am trying to see if it is possible to retain the features of VSCode PowerShell Extension but running and debugging the script in PowerShell terminal without worrying about different behaviors resulted from the extension terminal, that is, to use the PowerShell intellisense, syntax highlighting, etc. but running & debugging script in Windows PowerShell terminal from VSCode.
Tried
I have tried running scripts in both VSCode PowerShell Extension terminal & Windows PowerShell terminal to observe the behaviors. I have also tried researching useful tips and suggestions for running & debugging PowerShell script from VSCode in windows terminal, but most of the documentations are related to running & debugging script in VSCode PowerShell Extension.
Tool Version
VSCode == 1.73.0 (user setup)
Windows PowerShell == 5.1.19041.1682
VSCode PowerShell Extension == v2022.10.0
PowerShell Modules Version
PowerShellEditorServices.VSCode == 0.2.0
PowerShellEditorServices.Commands == 0.2.0
MicrosoftPowerBIMgmt.Profile == 1.2.1104
MicrosoftPowerBIMgmt.Admin == 1.2.1104

Can I detect in Powershell that I am running in VS Code's integrated terminal?

I would like to modify the standard PowerShell profile in Windows if the Powershell opens inside VS Code integrated terminal (when you are editing e.g. python scripts in VS Code, rather than PS scripts, which opens the ISE profile in any case).
Is there some environmental variable that gets set by the integrated PowerShell? Or is there some way of opening Powershell with a particular profile, instead of the default?
Thanks
VS Code creates an environment variable named TERM_PROGRAM. You can check it for a value of vscode, something like this:
if($env:TERM_PROGRAM -eq 'vscode') {
# do some stuff...
}
If you want to check if you're running within PowerShell Integrated Console (ships with PowerShell extension) under vscode and not just any powershell console running under vscode, you can:
if ($Host.Name -eq 'Visual Studio Code Host') {
Write-Output 'PowerShell Integrated Console'
}
This is meaningful to detect because it is the only powershell console host that provided full debugger support (eg. break on exception experience) and debugger integration with vscode.
You said you're into modifying profile when running within vscode, then you should check (again with PowerShell integrated console which ships with PoweShell extension):
PS> $PROFILE.CurrentUserCurrentHost
C:\Users\username\Documents\PowerShell\Microsoft.VSCode_profile.ps1
PS> $PROFILE.AllUsersCurrentHost
C:\Program Files\PowerShell\7\Microsoft.VSCode_profile.ps1
If people are working on PowerShell code within Visual Studio Code, then why they would NOT install PowerShell extension which ships with a specific console that fully integrates with Visual Studio code and on the top of that, provides you a profile file specifically geared towards Visual Studio code?

PowerShell ISE: how to run new PowerShell version

How can I get PowerShell ISE to work with PowerShell 6.0. Currently, it has 4.0.
This server has PowerShell 4.0 installed and I installed PowerShell 6.0 with PowerShell-6.1.0-win-x64.msi from this link: https://github.com/PowerShell/PowerShell/releases
The files now exist in C:\Program Files\PowerShell\6.
However, ISE still shows 4.0, but I need it to run 6.0
$PSVersionTable.psversion
Major Minor Build Revision
4 0 -1 -1
PowerShell has 3 executables...
Windows only...
PowerShell_ISE.exe = ISE v2 - v5.1
powershell.exe = consolehost v1 - 5.1
Note: (there are no new versions coming, but this will be supported going forward as it is today, as per Microsoft announcements, just nothing new coming to it. Just bug /security fixes.)
Cross-platform
pwsh.exe = PowerShell Core/v6
The editor to use if you are using v6 is Visual Studio Code, which is also cross-platform, unlike the ISE.
https://code.visualstudio.com/Download
The are no plans to make the ISE leverage pwsh.exe/PSCore.
However, you can continue to use the ISE, and if you need something specifically to leverage PSCore, you have to shell out to it just as you would to cmd.exe. Your code will run the pwsh.exe console, and return you back to the ISE, depending on how you shell out to it.
What you are using is Windows PowerShell ISE which is always has Windows PowerShell as the integrated shell, you need VSCode here, where you can select which version of PowerShell to use ...
Just need to change below item in File -> Preferences -> Settings.
"terminal.integrated.shell.windows": "C:\\Program Files\\PowerShell\\pwsh.exe"
Same can be done via UI in few clicks as well...

Where does the version number in PowerShell title refer to?

In this post I found the way to detect the installed Powershell version: Determine installed PowerShell version.
When I type "powershell" into "Execute as" (Win+R) the powershell opens with the title:
C:\Windows\System32\WindowsPoershell\v1.0\powershell.exe
However, the commands from the linked SO question deliver following output:
So my question: Why is there v1.0 in the title of the shell window whereas the other commands tell me that I have version 2?
Your PowerShell version is 2. The title shows the path to the PowerShell executeable. I think the reason why all new PowerShell version are located there (in v1.0) is because so many cmd scripts invoking some PowerShell code using this file path (e. g. Scheduled Tasks). But I may be wrong.
Another reason could be, that its not possible to run two different PowerShell Versions side-by-side.

Is there a way to change the shell that opens when using keybinding Ctrl + Shift + c in Visual Studio Code?

I've noticed this code in the keyboard shortcut settings.
{ "key": "ctrl+shift+c", "command":"workbench.action.terminal.openNativeConsole" },
I was wondering, instead of opening cmd.exe, can I change it to open Powershell?
Starting in v1.1.0, we can now configure the external shell.
Before version 1.6.1, there was only one setting.
"externalTerminal.windowsExec": "powershell"
Starting with version 1.6.1, there is an external and internal terminal setting. Using the value "%COMSPEC%", one could instead change their COMSPEC environment variable.
// The path of the shell that the terminal uses on Windows. When using shells shipped with Windows (cmd, PowerShell or Bash on Ubuntu), prefer C:\Windows\sysnative over C:\Windows\System32 to use the 64-bit versions.
"terminal.external.windowsExec": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
// The path of the shell that the terminal uses on Windows. When using shells shipped with Windows (cmd, PowerShell or Bash on Ubuntu), prefer C:\Windows\sysnative over C:\Windows\System32 to use the 64-bit versions.
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
I found, however, that it opens the 32bit Powershell.exe. Which, for me, did not have its execution policy set. So I set it using the following...
I elevated my permissions (Powershell's version of sudo):
Start-Process Powershell -Verb Runas
Then, in the new elevated Powershell window:
Set-ExecutionPolicy RemoteSigned
At the moment, the customization is more geared towards changing the keys / conditions that trigger the actions, rather than adding new actions.
It would be great to be able to create custom actions too - and the Visual Studio Code team are interested in hearing ideas on their User Voice site.
I have added a suggestion for custom actions.
There is still no setting yet as of February 2016, but you can use the CodeShell extension and once installed use Ctrl-Alt-P to launch a PowerShell window at the root of the project folder.