Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Visual Studio Code Powershell Terminal in script folder

When I right-click a ps1 script in VSCode and open it in Powershell Terminal, the current folder is not the script folder but the project folder.

Is there a way to configure it to open in the script folder ?

like image 362
user310291 Avatar asked Feb 28 '26 15:02

user310291


1 Answers

Assumptions:

  • You've configured PowerShell as the default shell - either via command Terminal: Select Default Shell (on Windows) or directly via user settings "terminal.integrated.shell.windows" / "terminal.integrated.shell.osx" / "terminal.integrated.shell.linux".

  • You're trying to open a PowerShell instance by right-clicking a file / subfolder in the side bar's Explorer view and choose Open in Terminal, and you want that instance's current location to be that file's containing folder / that subfolder.

As of (at least) VSCode 1.17.2, this should work by default.

If it doesn't, perhaps custom code in your $PROFILE directly or indirectly changes the current location.

If you don't want to / cannot prevent $PROFILE code from changing the location, here's a workaround via the user settings that explicitly sets the current location after the $PROFILE code has run:

On Windows:

"terminal.integrated.shell.windows": "cmd.exe",
"terminal.integrated.shellArgs.windows": [ "/c", "powershell -noexit -command 'Set-location \"%CD%\"'" ]

This uses cmd.exe as the default shell, which then invokes PowerShell with a command that explicitly makes it change to that folder.

The workarounds for other platforms below work analogously:

On macOS:

"terminal.integrated.shell.osx": "/bin/bash",
"terminal.integrated.shellArgs.osx": [ "-l", "-c", "exec pwsh -noexit -command 'set-location \"$PWD\"'" ]

On Linux:

"terminal.integrated.shell.linux": "/bin/bash",
"terminal.integrated.shellArgs.linux": [ "-c", "exec pwsh -noexit -command 'set-location \"$PWD\"'" ]
like image 157
mklement0 Avatar answered Mar 02 '26 13:03

mklement0