Reputation: 115
I'm working on a project that requires me to compile C++ code using MSVC, but I am working mostly with VSCode. As such, I was wondering if there is a way for me to add the Developer Powershell as an integrated terminal, so that I can compile without needing a secondary terminal open. I thought of just opening VSCode from the Developer PS itself, but since this is mostly a temporary project it seemed like a lot of repetitive work. I tried using the Shell launcher
extension for VSCode but it didn't work. Is there anything I can do?
Upvotes: 7
Views: 5861
Reputation: 439822
Update:
The answer below is obsolete. For a current solution, see Elon Mallin's answer, for instance, which uses the dedicated Launch-VsDevShell.ps1.ps1
script that has since been introduced, and shows a JSON object that you can place inside the "terminal.integrated.profiles.windows"
property in settings.json
(create the property if not already present), which makes a shell named Developer PowerShell for VS 2022
available in the integrated terminal.
That said, there are some subtleties to consider, and no single answer addresses them all as of this writing; a robust formulation[1] requires the following; note that the Launch-VsDevShell.ps1
script's path changes between Visual Studio versions, and varies based on whether a given installation is 32-bit vs. 64-bit as well as the edition name (Community, Professional, ...) the following hard-codes the path for the Professional edition of Visual Studio 2022 - adjust as needed:
// 'settings.json' excerpt
// (Open the file via the command palette with
// "Preferences: Open User Settings (JSON)"):
// Create this property, if necessary; if it already exists,
// add the nested "Developer PowerShell for VS 2022 Professional"
// property as a new property to it.
"terminal.integrated.profiles.windows": {
// Preexisting profiles, if any...
"Developer PowerShell for VS 2022 Professional": {
"overrideName": true,
"source": "PowerShell",
"icon": "terminal-powershell",
"args": [
"-NoExit",
"-File",
"C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/Tools/Launch-VsDevShell.ps1",
"-SkipAutomaticLocation"
]
}
}
Obsolete, original answer:
To make Visual Studio Code's integrated terminal act like the Developer PowerShell for VS 2019
console that comes with Visual Studio 2019, add the following to your Visual Studio Code settings.json
file (> Preferences: Open Settings (JSON)
):
"terminal.integrated.shell.windows": "C:/Windows/SysWOW64/WindowsPowerShell/v1.0/powershell.exe"
and
"terminal.integrated.shellArgs.windows": "-noe -c Import-Module 'C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/Tools/Microsoft.VisualStudio.DevShell.dll'; Enter-VsDevShell ed9e071d"
Note that a 32-bit version of PowerShell is started, followed by import of a module and a call to a function from that module.
I've taken (and adapted) the commands - whose details may differ depending on the Visual Studio version - from the Properties dialog of the following shortcut file (*.lnk
):
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2019\Visual Studio Tools\Developer PowerShell for VS 2019.lnk
[1] For robustness, the -File
parameter of the PowerShell CLI must be used; without -File
, it only works if you happen to have the install-on-demand PowerShell (Core) 7 edition of PowerShell installed, whose CLI (pwsh.exe
) now defaults to the -File parameter. By contrast, the ships-with-Windows , legacy Windows PowerShell edition (whose CLI is powershell.exe
), -Command
is implied, which breaks calling a script path that contains spaces. Visual Studio Code, with a "source"
property value of "PowerShell"
, uses PowerShell (Core) 7 if found installed, and Windows PowerShell otherwise.
As for the working directory: Launch-VsDevShell.ps1
sets its own working directory, unless suppressed via -SkipAutomaticLocation
. Thus, -SkipAutomaticLocation
must be passed in order for the Visual Studio Code-determined working directory to take effect.
Optionally - in PowerShell (Core) 7 only - you may control the working dir. via the PowerShell CLI's -WorkingDirectory
parameter, but note that passing ${workspacefolder}
only works if Visual Studio Code was given a folder or workspace argument on startup.
Finally, if you wanted to control the working directory via Windows PowerShell, you'd have to switch to a -Command
-based CLI call that uses an in-session Set-Location
(cd
) call.
Upvotes: 4
Reputation: 2181
Tried existing answers, not works perfectly.
My working config is the following, which navigate to current working directory (the root directory that VSCode opens):
// https://learn.microsoft.com/en-us/visualstudio/ide/reference/command-prompt-powershell?view=vs-2022
"Developer PowerShell for VS 2022": {
"overrideName": true,
"source": "PowerShell",
"icon": "terminal-powershell",
"args": [
"-NoExit",
"& \"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\Tools\\Launch-VsDevShell.ps1\"",
"-SkipAutomaticLocation",
"-Arch", "amd64",
"-HostArch", "amd64"
]
}
Upvotes: 0
Reputation: 14024
I found this in March of 2023, looking for an answer to this question. At this point, Microsoft documents a Launch-VsDevShell.ps1
script that is the recommended way to start a developer PowerShell terminal. I tried simply making that script the path
parameter in the above JSON, but that didn't work. Then I tried making it the sole member of args
, and that seemed to work briefly and then exit. Finally, I added -NoExit
and that seems to work like a charm!
Of note for anyone coming after me, I'm using Visual Studio Community 2022 with an x86-64 install (so it's under C:\Program Files\
.
"Developer PowerShell for VS 2022": {
"source": "PowerShell",
"icon": "terminal-powershell",
"args": [
"-WorkingDirectory",
"${workspaceFolder}",
"-NoExit",
"C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/Tools/Launch-VsDevShell.ps1"
]
}
Upvotes: 9
Reputation: 11
I had issues with Windows style paths on my machine and I have only installed Build Tools so the path is changed as well. My settings.json looks like this now:
"Developer PowerShell for VS 2022": {
"overrideName": true,
"source": "PowerShell",
"icon": "terminal-powershell",
"args": [
"-NoExit",
"& \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\Common7\\Tools\\Launch-VsDevShell.ps1\"",
]
}
Additionally I had to change the Execution-Policy for PS scripts:
Set-ExecutionPolicy -ExecutionPolicy Bypass
Execute in Admin Powershell at your own risk.
Upvotes: 1
Reputation: 353
Expanding on all answers here to make it behave a little more as expected. Add this to your settings.json:
"terminal.integrated.profiles.windows": {
"Developer PowerShell for VS 2022": {
"overrideName": true,
"source": "PowerShell",
"icon": "terminal-powershell",
"args": [
"-WorkingDirectory",
"${workspaceFolder}",
"-NoExit",
"C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/Tools/Launch-VsDevShell.ps1",
"-SkipAutomaticLocation"
]
}
},
The -WorkingFolder ${workspaceFolder}
and SkipAutomaticLocation
will make the terminal start in your current workspace.
The "overrideName": true
will override the default pwsh
name in the list of terminals once it's opened and name it Developer PowerShell for VS 2022
:
I couldn't find a way to dynamically get the Visual Studio installation directory unfortunately so everyone will have to put in their absolute path.
Upvotes: 0
Reputation: 475
Update for Visual studio 2022 on my machine
"terminal.integrated.profiles.windows": {
"Developer PowerShell for VS 2022": {
"source": "PowerShell",
"icon": "terminal-powershell",
"args": [
"-noe",
"-c",
"&{Import-Module 'C:/Program Files (x86)/Microsoft Visual Studio/2022/Community/Common7/Tools/Microsoft.VisualStudio.DevShell.dll'; Enter-VsDevShell ed9e4c07}"
]
}
}
Upvotes: 4
Reputation: 159
A variation of the answer of mklement0 is to use terminal.integrated.profiles.windows
in the Visual Studio Code settings.json
like this:
"terminal.integrated.profiles.windows": {
"Developer PowerShell for VS 2019": {
"source": "PowerShell",
"icon": "terminal-powershell",
"path": "{env:windir}\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe",
"args": [
"-noe",
"-c",
"&{Import-Module 'C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/Tools/Microsoft.VisualStudio.DevShell.dll'; Enter-VsDevShell 7068d947}"
]
}
}
Upvotes: 6