Open In Terminal feature does not open specific folder. #19495

Closed
opened 2026-01-31 06:45:06 +00:00 by claunia · 6 comments
Owner

Originally created by @GarThor on GitHub (Mar 6, 2023).

Windows Terminal version

1.16.10261.0

Windows build number

10.0.19042.0

Other Software

PowerShell v5

Side Note: I have a PowerShell profile set up that sets the default location for where PowerShell should start, using the "Set-Location" cmdlet. The "open PowerShell window here" context menu feature seems to bypass that "Set-Location" which is what is desired in this scenario.

Steps to reproduce

  1. right-click folder
  2. select open-in-terminal
  3. Terminal opens, but not in the directory selected

Expected Behavior

Terminal opens in selected directory, the same as if I had pressed shift-rightclick->open powershell window here

Actual Behavior

Terminal opens in default directory (set in PowerShell profile, using the "Set-Location" cmdlet)

Originally created by @GarThor on GitHub (Mar 6, 2023). ### Windows Terminal version 1.16.10261.0 ### Windows build number 10.0.19042.0 ### Other Software PowerShell v5 Side Note: I have a PowerShell profile set up that sets the default location for where PowerShell should start, using the "Set-Location" cmdlet. The "open PowerShell window here" context menu feature seems to bypass that "Set-Location" which is what is desired in this scenario. ### Steps to reproduce 1) right-click folder 2) select open-in-terminal 3) Terminal opens, but not in the directory selected ### Expected Behavior Terminal opens in selected directory, the same as if I had pressed shift-rightclick->open powershell window here ### Actual Behavior Terminal opens in default directory (set in PowerShell profile, using the "Set-Location" cmdlet)
claunia added the Issue-QuestionNeeds-TriageIssue-BugResolution-Answered labels 2026-01-31 06:45:07 +00:00
Author
Owner

@zadjii-msft commented on GitHub (Mar 7, 2023):

Wait hold up what exactly is in your powershell profile/?

If I'm understanding you right:

  • You right-click on a folder to "Open Terminal here"
  • That spawns wt -d <the directory>
  • Your default profile is PowerShell so that launches (presumably, initially in the right directory)
  • PowerShell executes your profile.ps1, which calls Set-Location <somewhere else>
  • The shell apparently starts in <somewhere else>

That all sounds like it's working exactly as I'd expect it to, right?

@zadjii-msft commented on GitHub (Mar 7, 2023): Wait hold up what exactly is in your powershell profile/? If I'm understanding you right: * You right-click on a folder to "Open Terminal here" * That spawns `wt -d <the directory>` * Your default profile is PowerShell so that launches (presumably, initially in the right directory) * PowerShell executes your profile.ps1, which calls `Set-Location <somewhere else>` * The shell apparently starts in `<somewhere else>` That all sounds like it's working exactly as I'd expect it to, right?
Author
Owner

@DHowett commented on GitHub (Mar 7, 2023):

So, this is an interesting case study in how Terminal's "disavowing" of any shell knowledge makes this not work too well.

PowerShell's built-in "open in powershell" shell extension launches powershell ... -c set-location c:\foo\bar. That command runs after the profile.

Terminal can't promote the starting directory to an after-the-profile Set-Location because it doesn't actually know, to any meaningful extent, that it is launching something that looks like PowerShell.

@DHowett commented on GitHub (Mar 7, 2023): So, this is an interesting case study in how Terminal's "disavowing" of any shell knowledge makes this not work too well. PowerShell's built-in "open in powershell" shell extension launches `powershell ... -c set-location c:\foo\bar`. That command runs after the profile. Terminal can't promote the starting directory to an after-the-profile `Set-Location` because it doesn't _actually know_, to any meaningful extent, that it is launching something that looks like PowerShell.
Author
Owner

@GarThor commented on GitHub (Mar 8, 2023):

Yep, pretty much @zadjii-msft. There's some documentation on how powershell profiles work here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-5.1

Interesting @DHowett ... I wonder if there's a workaround I can implement in my profile script, where it detects if it's running within the terminal application rather than standalone?

@GarThor commented on GitHub (Mar 8, 2023): Yep, pretty much @zadjii-msft. There's some documentation on how powershell profiles work here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-5.1 Interesting @DHowett ... I wonder if there's a workaround I can implement in my profile script, where it detects if it's running within the terminal application rather than standalone?
Author
Owner

@GarThor commented on GitHub (Mar 8, 2023):

I think I've come up with a reasonable workaround.

To my powershell configuration in the terminal app, I've added the following command line params to tell it specifically to not load any profiles at all, and then execute a specific script, which does everything that my normal profile does, excpet setting the directory.

\powershell.exe -noprofile -noexit -file "\terminal_profile.ps1"

It'd be nice if it worked out of the box, but this seems to work for now...

@GarThor commented on GitHub (Mar 8, 2023): I think I've come up with a reasonable workaround. To my powershell configuration in the terminal app, I've added the following command line params to tell it specifically to _not_ load any profiles at all, and then execute a specific script, which does everything that my normal profile does, excpet setting the directory. <path-to-powershell>\powershell.exe -noprofile -noexit -file "<path-to-terminal-specific-profile>\terminal_profile.ps1" It'd be nice if it worked out of the box, but this seems to work for now...
Author
Owner

@DHowett commented on GitHub (Mar 8, 2023):

I think that's an acceptable workaround. The problem is, I don't think we can fix it out-of-the-box. Having a profile that always modifies inherited state (like the location! but also the console window size and a couple other bits) isn't tenable in the long run.

There is a category of (poorly-authored) build scripts that forget to pass -NoProfile when they shell out to PowerShell to do something; there are also applications that spawn PowerShell as a subshell to do something that also don't pass -NoProfile. I have a debug print in my own profile, and it shows up in the strangest places. Each of those things is utterly unprepared for state pollution.

In short, there are way more of those than there are instances of your profile in existence. 😄

@DHowett commented on GitHub (Mar 8, 2023): I think that's an acceptable workaround. The problem is, I don't think we can fix it out-of-the-box. Having a profile that always modifies inherited state (like the location! but also the console window size and a couple other bits) isn't tenable in the long run. There is a category of (poorly-authored) build scripts that forget to pass `-NoProfile` when they shell out to PowerShell to do something; there are also applications that spawn PowerShell as a subshell to do something that also don't pass `-NoProfile`. I have a debug print in my own profile, and it shows up in the strangest places. Each of those things is utterly unprepared for state pollution. In short, there are way more of those than there are instances of your profile in existence. :smile:
Author
Owner

@GarThor commented on GitHub (Mar 17, 2023):

That workaround didn't really work either... so I added an if-then clause... if it starts up in the regular working directory it switches to the one I define... otherwise it goes with whatever directory I selected in explorer.

I also had to add a clause in my profile to refresh environment variables, because apparently that doesn't happen on new tab either, but that's a separate issue.

@GarThor commented on GitHub (Mar 17, 2023): That workaround didn't really work either... so I added an if-then clause... if it starts up in the regular working directory it switches to the one I define... otherwise it goes with whatever directory I selected in explorer. I also had to add a clause in my profile to refresh environment variables, because apparently that doesn't happen on new tab either, but that's a separate issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#19495