Powershell 7 Windows Explorer context menu options to open a shell at the current directory do not work properly (with fix) #14091
Labels
Area-Maintainers-Build
specific to affecting the build
Issue-Question
ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
Resolution-No Activity
Issue has had no activity for 6 months or more
Steps to reproduce
Expected behavior
Actual behavior
Fix
The problem here is that the registry command sets the WorkingDirectory, but that is not the same as the directory where the shell is opened to. You also need to do a Set-Location.
This is the command as laid down by WIX in Product.wxs
C:\Program Files\PowerShell\7\pwsh.exe -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%V!" -Command "$host.UI.RawUI.WindowTitle = 'PowerShell 7 (x64)'"
This is a command that actually works:
[HKEY_CLASSES_ROOT\Directory\ContextMenus\PowerShell7x64\shell\openpwsh\command]
@="C:\Program Files\PowerShell\7\pwsh.exe -NoExit -RemoveWorkingDirectoryTrailingCharacter -WorkingDirectory "%V!" -Command "$host.UI.RawUI.WindowTitle = 'PowerShell 7 (x64)'; $wdtemp=[Environment]::CurrentDirectory; Set-Location -Path $wdtemp""
As noted above, you need to do a Set-Location to get the shell to open in the desired directory. This is complicated by the issue that -RemoveWorkingDirectoryTrailingCharacter was designed to fix, but that switch does not work with Set-Location. The solution was to set the -WorkingDirectory with -RemoveWorkingDirectoryTrailingCharacter (which sets [Environment]::CurrentDirectory properly), then read that into a variable and pass that to Set-Location.
Since this is a strange sequence of commands and there might be a better way to do it, I'm leaving the final fix to the experts, but this does produce the desired behavior.
Environment data
The text was updated successfully, but these errors were encountered: