0

My question is the exact opposite of the question How to open windows explorer from current working directory of WSL shell?.

When I am at specific directory in Windows explorer, typing cmd from the address bar opens the cmd at that directory. In this example working directory is C:\users.

enter image description here

Is there a way to do similarly to to open wsl shell command line at the working File explorer? That is, to open the below wsl command line:

enter image description here

Typing and running just bash or wsl from the address bar, open the wsl command line, but not at the current working directory.

Name
  • 433
  • 1
    Typing just wsl into File Exploer does open in the directory that Explorer is viewing for me. Do you perhaps have something in one of your startup scripts (e.g. ~/.bashrc) that changes the directory? – NotTheDr01ds Jul 26 '22 at 14:36
  • Hmm - Re-reading your question, it works when you first start cmd, then wsl from within that CMD session, so that would mean that it couldn't be a startup file that's overriding it. Not sure then ... – NotTheDr01ds Jul 26 '22 at 14:38
  • Also .. bash pulls of the same thing for me in addition to WSL. – Señor CMasMas Jul 26 '22 at 15:31
  • @NotTheDr01ds I edited the question accordingly. You are right, it seems that even through cmd then running wsl or bash I cannot go to that directory. – Name Jul 26 '22 at 15:36
  • @SeñorCMasMas I edited the question accordingly. – Name Jul 26 '22 at 15:37
  • @Name Okay, then that does sound like something in a startup file is changing it. From CMD, if you run wsl -e bash --noprofile --norc, does it then start in the current working directory? If so, then that would be fairly conclusive, I believe, that something in one of your startup files is causing the issue. – NotTheDr01ds Jul 26 '22 at 16:04
  • @NotTheDr01ds wsl -e bash --noprofile --norc starts in the root of wsl, the directory containing bin boot dev etc home init lib lib32 lib64 libx32 media mnt opt proc root run sbin snap srv sys tmp usr var. – Name Jul 26 '22 at 16:54
  • @Name Okay, that seems to indicate that it is not something to do with the startup config then. Does the same happen from PowerShell? I expect that it will, but running out of ideas ... – NotTheDr01ds Jul 26 '22 at 18:18
  • @NotTheDr01ds I confirm that when from powershell I fire bash or wsl, it starts in the current directory. – Name Jul 26 '22 at 19:23
  • @Name Curiouser and curiouser ... Okay, so PowerShell works as expected, but CMD does not. Does where wsl (in CMD) and get-command wsl (in PowerShell) point to the same location? – NotTheDr01ds Jul 26 '22 at 21:12
  • Also, doskey /macros (in CMD) doesn't show WSL in there, right? – NotTheDr01ds Jul 26 '22 at 21:13
  • @NotTheDr01ds where wsl and doskey /macros give no result. – Name Jul 27 '22 at 03:52

1 Answers1

0

Instead of "cmd" write "wsl" in the adress bar.


Second try:

Sorry, I have overseen the last line in the original text, probably because it was after the screenshots. Please, use the following command on the command line to check if your registry has the following keys:

  1. WSL-key:
reg query HKCR\Directory\shell\WSL

The content on my computer (unchanged from me) is:

HKEY_CLASSES_ROOT\Directory\shell\WSL
    (Default)    REG_SZ    @wsl.exe,-2
    Extended    REG_SZ
    NoWorkingDirectory    REG_SZ
  1. The starting command:
reg query HKCR\Directory\shell\WSL\command

The content should be:

HKEY_CLASSES_ROOT\Drive\shell\WSL\command
    (Default)    REG_SZ    wsl.exe --cd "%V"

As you can see, here is a "hiden" change-directory-command (cd) and a parameter.

Similarly, query the respective key for drives:

reg query HKCR\Drive\shell\WSL\command

If you get the following error:

ERROR: The system was unable to find the specified registry key or value.

something is wrong with your installation of wsl. Changing the values of the mentioned keys could solve the problem, but is not trivial, because they are protected and only the trusted installer can change them. IMHO it will be easier to deactivate the WSL in the windows features, reboot, activate it again and reboot again.

A workaround (and probably easier solution) can be to define a new key

reg add HKCR\Directory\shell\WSL_here\command /ve /d "wsl.exe --cd \"%V\""

in a command-prompt-window with elevated privilege, of course. This should insert in Windows explorer immediately a new entry in the right-click menu on any directory, which would start a wsl in that directory by selecting/clicking.

su_hits
  • 1
  • 1
  • It could be that the context menu was extended with this command from the Microsoft's "PowerToys" .. Context menu entries like this aren't complicated. Like 2 registry keys "not that complicated". Could simply be added for "WSL Bash Here" but this isn't what the OP asked for. – Señor CMasMas Jan 31 '23 at 15:16
  • Just try it. It really opens the WSL in the terminal, and the current directory is as desired. It does not work, though, for UNC directories, only for the local drives! – su_hits Feb 09 '23 at 12:25