How to Change Visual Studio Code 'Powershell Integrated Console'? - powershell

Does anyone have any idea on how to install/update PS 7 to be utilized by the VS Code Powershell Integrated Console. I can get PS 7 on the regular powershell terminal, however the Powershell Integrated Console is still PSVersion 5.1. I cannot seem to figure out how to change this. Any help would be much appreciated.
My settings.json is below:

I'm using this code and it's working for me.
"powershell.powerShellAdditionalExePaths": [
{
"exePath": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
"versionName": "Downloaded PowerShell"
}
],
"powershell.powerShellDefaultVersion": "Downloaded PowerShell",
"terminal.integrated.shell.windows": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",

Powershell 7.x (a.k.a. pwsh.exe, formerly known as Powershell Core) is now default shell in Visual Studio 2022. I think it should be the default Powershell version in VSCode (if extension detects Powershell 7+ is present). If user has it installed I think it should usually be the default now.
{
"powershell.powerShellAdditionalExePaths": [{
"exePath": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
"versionName": "PowerShell 7.x customized"
}],
"powershell.powerShellDefaultVersion": "PowerShell 7.x customized",
}

TLDR; specify the default profile that you would like to be loaded for your terminal in the settings.json file:
{
"terminal.integrated.defaultProfile.windows": "PowerShell",
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell"
}
}
}
The terminal setup has changed somewhat since the question was first asked.
The setting terminal.integrated.shell.windows has been deprecated since, and is replaced by Terminal Profiles.
Also, the behaviour changes if you install the PowerShell extension. The extension adds its "own" integrated terminal experience. But specifying the 'default' profile should have the terminal start up with the profile that is specified.
Notice my setting sets the default as PowerShell (aka Powershell Core), not Windows PowerShell

PowerShell for VS Code maintainer here. We're so sorry, but we accepted a PR that ended up being a breaking change to this setting. Notably this change provides support for configuring the setting via the GUI. This PowerShell Additional Exe Paths setting should now be configured as a dictionary of key/value pairs, like so:
{
"powershell.powerShellAdditionalExePaths": {
"Downloaded PowerShell": "C:/Users/username/Downloads/PowerShell/pwsh.exe",
"Built PowerShell": "C:/Users/username/src/PowerShell/src/powershell-win-core/bin/Debug/net6.0/win7-x64/publish/pwsh.exe"
},
"powershell.powerShellDefaultVersion": "Downloaded PowerShell",
}

Related

How to integrate Developer Command Prompt for VS 2022 as Terminal in VS Code

I would like to have the Microsoft VS C++ compiler cl available in VisualStudio Code.
I have Visual Studio Build Tools installed and can call cl from the Developer Command prompt.
The default way recommended by Microsoft to get VS Code to use the cl compiler is to call VS Code from the Developer Command prompt.
I would like to do it differently, with a terminal that I can call from VS Code and make it the default terminal if needed.
In Windows Terminal, I get the automatically generated Developer Command prompt entry with the command line:
cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\Tools\VsDevCmd.bat" -arch=x64 -host_arch=x64`
or
powershell.exe -NoExit -Command "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell 0d0637f3 -SkipAutomaticLocation -DevCmdArguments """-arch=x64 -host_arch=x64"""}"
If I paste this line into an existing vscode terminal, it works and I can use the cl compiler. But I can't manage to get it into an integrated terminal in settings.json.
This is what I tried in the settings.json:
"Developer Command Prompt for VS 2022": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe /k \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\Common7\\Tools\\VsDevCmd.bat\" -arch=x64 -host_arch=x64",
"${env:windir}\\System32\\cmd.exe /k \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\Common7\\Tools\\VsDevCmd.bat\" -arch=x64 -host_arch=x64"
],
"overrideName": true,
"args": [],
"icon": "terminal-cmd"
},
Since VScode is pretty smart, it recognizes that this won't work and doesn't even list it as an entry within the available terminals.
Furthermore, i have absolutely no idea what to do with the commandline given for PowerShell, notably the three doublequotes after Import-Module and how to handle them.
Try the following to load VsDevCmd.bat in cmd.exe from the VSCode integrated terminal.
For running it through PowerShell, you should be able to just replace the path with PowerShell, and alter the arguments to match accordingly (/k would become -NoExit, etc).
"VsDevCmd (2022)": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe",
"${env:windir}\\System32\\cmd.exe"
],
"args": [
"/k",
// Path below assumes a VS2022 Community install;
// update as appropriate if your IDE installation path
// is different, or if using the standalone build tools
"C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/Tools/VsDevCmd.bat",
"-arch=x64",
"-host_arch=x64"
],
"overrideName": true,
"icon": "terminal-cmd"
},

New way to specify Powershell 7 in Visual Studio Code?

I am trying to replace the default Powershell 5 with the newer Powershell 7, on Windows 11.
99% of the solutions on the internet say to add this to settings.json.
"terminal.integrated.shell.windows": "C:\\Program Files\\PowerShell\\7\\pwsh.exe"
However, this now gives a red squiggly line with the following message:
This is deprecated, the new recommended way to configure your default
shell is by creating a terminal profile in
#terminal.integrated.profiles.windows# and setting its profile name
as the default in #terminal.integrated.defaultProfile.windows#. This
will currently take priority over the new profiles settings but that
will change in the future.(2)
There is one possibly related thread, but it only deals with defaulting it to the native Command Prompt, rather than re-jigging things to Powershell 7.
So, what is the correct new way to provide Powershell 7s path to VS Code, and also set it as the default terminal?
In earlier VSCode (Visual Studio Code) versions, the "terminal.integrated.shell.*" and "terminal.integrated.shellArgs.*" settings determined the default shell and its startup arguments for the integrated terminal.
These have been superseded by shell profiles, defined via "terminal.integrated.profiles.*" properties, and an associated "terminal.integrated.defaultProfile.*" property that contains the name of the profile to use by default, as shown below (use > Preferences: Open Settings (JSON) from the command palette to open your settings.json file):
"terminal.integrated.profiles.windows": {
"PowerShell_7": {
"path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
"icon": "terminal-powershell"
}, // ...
}
// Make the profile defined above the default profile.
"terminal.integrated.defaultProfile.windows": "PowerShell_7"
Note:
The above defines the default general-purpose shell for Visual Studio Code's integrated terminal.
For information on how to specify what PowerShell version to use with the special-purpose PIC (PowerShell Integrated Console) that comes with the PowerShell extension (for authoring and debugging PowerShell code), see this answer.
I would have expected Visual Studio Code to use your v7 version automatically, as it - if installed - normally takes precedence over Windows PowerShell.

How to prevent VSCode to load powershell profile?

I have looked at VSCode settings, I can't see clearly what "integrated" means that's what I have but it loads external powershell profile. I want to prevent it to do so, is it possible ?
P.S. : I don't have and don't want powershell extensions, should I to have this option ?
Update: As suggested I have unchecked "powershell.enableProfileLoading" both in User and Workspace settings, relaunched vscode, but in terminal still gets "Loading personal and system profiles took" weirdly it still doesn't work for me ?
Is it possible this is a VSCode settings bug ?
If you DO have Visual Studio Code's PowerShell extension installed and you want to suppress loading of profiles in its special-purpose shell, the PowerShell Integrated Console (PIC):
GUI option: Via the Settings dialog, uncheck the option PowerShell: Enable Profile Loading
Settings.json file option: As Santiago Squarzon states, add the following setting:
"powershell.enableProfileLoading": false
If you do NOT have the PowerShell extension installed OR you (also) want to control profile loading for general-purpose PowerShell sessions running in Visual Studio's integrated terminal:
In the Settings dialog, search for terminal profiles and click on the Edit in settings.json link next to the platform-appropriate Terminal › Integrated › Profiles: <os> entry, where <os> is one of windows, linux or osx (macOS).
This will open your Settings.json file for editing and either create or navigate to a preexisting "terminal.integrated.profiles.<os>"object, which contains the profiles of all shells available in the integrated terminal.
Locate the PowerShell profile and add an "args": [ "-noprofile" ] property in order to suppress profile loading, which passes the -noprofile parameter to the PowerShell CLI (powershell.exe for Windows PowerShell, pwsh for PowerShell (Core) 7+) on startup; e.g., on Windows:
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell",
"args": [ "-noprofile" ]
},
// ...
}

Cmder integration in VS Code with PowerShell not working as expected

I'm trying to use Cmder in VS Code for PowerShell instead of powershell.exe. I followed the official instructions (here) on the cmder GitHub. Now, when I restart VS Code a terminal will be launched. Looks like this:
It's name is 'powershell' and it looks like Cmder but doesn't behave like it.
Perhaps I'm expecting something that is not designed to work but I was expecting 'CTRL+ Backspace' to delete words to the left. Instead I'm getting '^W':
Moreover, as soon as I open a .ps1 file a new terminal gets launched which is named 'PowerShell Integrated Console':
The behaviour of this terminal is the same as the 'powershell' one.
Here's my settings related to the integrated terminal and PowerShell in the settings.json:
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"terminal.integrated.shellArgs.windows": [
"-ExecutionPolicy",
"Bypass",
"-NoLogo",
"-NoProfile",
"-NoExit",
"-Command",
". 'C:\\Users\\fhe\\[REMOVED]\\Apps\\cmder_mini\\vendor\\profile.ps1'"
],
"terminal.integrated.rendererType": "auto",
"terminal.integrated.scrollback": 10000,
"powershell.scriptAnalysis.enable": true,
"powershell.integratedConsole.focusConsoleOnExecute": false,
"powershell.enableProfileLoading": true,
I notice that if I change it back to what the official guide suggests under the 'Run Cmder as the VS Code terminal' (here) I will get the expected behaviour when using CTRL+Backspace.
So my questions are:
Is Cmder integration in VS Code designed to provide the CTRL+Backspace functionality for PowerShell?
If so, what might I have configured incorrectly?
Thanks.

Does Visual Studio Code work with PowerShell in the Command Palette?

With Visual Studio Code (not the traditional Visual Studio IDEs), can you run PowerShell in the Command Palette? I ask because I commonly use it in the full IDE.
I have not seen PowerShell mentioned in the documentation, other than for basic syntax highlighting. I have tried it with no success. Is it unsupported, or is it an optional feature I can configure somehow?
Note: to those voting up the PowerGUI answer, it is not correct as it references the wrong edition of Visual Studio for this question. It is helpful if you are using the full IDE, but not the new editor named: Code.
This was the first problem I wanted to solve in VSCode. I did not find a way to
type PowerShell commands but I have found a way to create and run PowerShell tasks.
In the command palette type and choose Tasks: Configure Task Runner. It will
show you a json file configured for the default task runner.
I replaced the content with this
{
"version": "0.1.0",
"command": "PowerShell.exe",
"isShellCommand": true,
"args": [
"-NoProfile",
"Invoke-Build.ps1"
],
"tasks": [
{
"taskName": "Build",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "Test",
"isTestCommand": true,
"showOutput": "always"
},
{
"taskName": "Build, Test",
"showOutput": "always"
}
]
}
As a result in the command palette I can choose and run my predefined tasks (Build, Test, and combined Build, Test). I can add other tasks and probably bind them to some hotkeys. This is not exactly what I would like to have in VSCode for PowerShell but for the preview it is at least something.
P.S. This just my first experiment that somewhat worked. It is not perfect, more likely. There are many configuration parameters for this json file that I have not tried yet.
With version 0.10.1 of Visual Studio Code, you can run and debug PowerShell. Here is a sample launch.json that I use:
{
"version": "0.2.0",
"configurations": [
{
"name": "PowerShell",
"type": "PowerShell",
"program": "MyScript.ps1",
"args": "-Verbose"
}
]
}
Unfortunately, I cannot get the args to work (for more details, see: https://github.com/PowerShell/vscode-powershell/issues/24). Another problem I have is that Read-Host does not work with VS Code (for more details, see: https://github.com/PowerShell/vscode-powershell/issues/29). Definitely some rough edges.
Here's how you can configure the Powershell task to execute the currently opened .ps1 file without any Invoke-Build dependency:
{
"version": "0.1.0",
"command": "PowerShell.exe",
"isShellCommand": true,
"args": [
"${file}"
],
"tasks": [
{
"taskName": "Build",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "Test",
"isTestCommand": true,
"showOutput": "always"
},
{
"taskName": "Build, Test",
"showOutput": "always"
}
]
}
Note: This is just a slight modification of Roman's answer (My edit to his answer was rejected).
Open the Debug view, in the View Bar select Debug from the View menu or press Ctrl + Shift + D.
In the Launch Configuration dropdown (shown in the following screenshot), select Add Configuration...
The launch.json configuration will open in the editor, type PowerShell and select the debug configurations you want, as shown in the following screenshot.
Save the launch.json file and select the debug configuration you want from the Launch Configuration dropdown:
Now you can debug PowerShell scripts through VSCode.
The Scripting Guys wrote a comprehensive 2 part blog post on this topic, like everything else they write, it's well worth a read if you're new to VSCode and PowerShell.
https://blogs.technet.microsoft.com/heyscriptingguy/2017/02/06/debugging-powershell-script-in-visual-studio-code-part-1/
https://blogs.technet.microsoft.com/heyscriptingguy/2017/02/13/debugging-powershell-script-in-visual-studio-code-part-2/
EDIT: I am aware that this question is several years old, but I came across it before I found any up to date information on how to set up PowerShell debugging in VSCode
As of version 0.10.3 there is now a PowerShell extension written by Microsoft that will enable writing PowerShell with IntelliSense and highlighting in Visual Studio Code
http://blogs.msdn.com/b/powershell/archive/2015/11/17/announcing-windows-powershell-for-visual-studio-code-and-more.aspx
At least in V1.4 (July 2016) this has been made a lot simpler.
The relevant documentation can be found here:
https://code.visualstudio.com/docs/editor/integrated-terminal
Essentially all you're doing is adding an entry to the settings.json file for User Settings.
Adding the following value:
// 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows":"C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe"
IMG: Shows the User Settings settings.json file and the successful Integrated Console showing the PowerShell prompt:
An "Integrated Terminal" is now integrated into Visual Studio Code. This is a PowerShell Terminal in Windows. (Likely BASH in Linux / MacOS). The terminal appears in a panel at the bottom of the code editor, not in the Command Pallete.
Keyboard Shortcut to open Terminal: CTRL + `
Sources:
https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf
https://code.visualstudio.com/docs/getstarted/keybindings
I confirmed in version 1.19.2. Not sure when the feature was first integrated.
There is a much easier way to run PowerShell, no configuration needed:
Install the Code Runner Extension
Open the PowerShell code file in Text Editor, then use shortcut Ctrl+Alt+N, or press F1 and then select/type Run Code, or right click the Text Editor and then click Run Code in context menu, the code will run and the output will be shown in the Output Window.
Besides, you could select part of the PowerShell code and run the code snippet. Very convenient!
PowerGUI tool provides a nice interface to Visual Studio. The goal of this extension is to bring PowerShell development into Visual Studio.
Here is how it looks -
Along with IntelliSense, the PowerGUI Visual Studio Extension provides the following features and more to make it easier to work with PowerShell.
PowerShell file and project types: You can create/edit PowerShell code files and assemble them into projects with more than one file.
PowerShell code snippets: The code snippet feature can be used for PowerShell code.
PowerShell console window: This feature provides the PowerShell console environment within Visual Studio IDE. This allows you to run commands or view output of scripts. Figure B shows the console window opened in the IDE.
PowerShell debugging: This feature is why I installed the extension; it provides a way to debug scripts within Visual Studio. It's a straight-forward way to locate syntax or logical problems in a script.
Syntax highlighting and script analysis: These are more Visual Studio features made available to PowerShell development.
In order to install the PowerGUI Visual Studio Extension, PowerGUI must be installed, with the caveat that the correct version of each product is installed.
Download Here
It also provides Debugging facility which is the best part I like as a developer.
Thanks!