Share

PowerShell, Explorer, square brackets and Unicode paths

Let’s make a folder C:\[test] and try to open PowerShell there from Explorer (using Ribbon or context menu).

set-location: Cannot find path 'C:[test]' because it does not exist. At line:1 char:1

Oops!

Let’s see what we can do with this.


Square brackets

Square brackets are special characters in PowerShell. There are workarounds like -LiteralPath or double backtick to access such folders. But Windows Explorer is totally unaware of them, sadly. The only thing you can do is to go one level up in folder structure, start PowerShell from there and then Set-Location (cd) to required folder — autocomplete is smart enough at least to insert backticks wherever they needed.

But that’s not really convenient.

And if you try to open the folder in CMD and then run PowerShell from there — it will fail to set proper location and end up being in PowerShell’s folder.

C:\[test]>powershell
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

[Killy] 03/18/2017 00:47:56

PS C:\WINDOWS\System32\WindowsPowerShell\v1.0
>

In CMD, environment variable %CD% holds a current path. Let’s pass it to Set-Location (cd) cmdlet when starting PowerShell:

C:\[test]>powershell -NoExit -Command "cd -LiteralPath '%CD%'"
[Killy] 08/25/2016 19:11:30

PS C:\[test]
>

It works. Now we can fix the folder context menu item, which is located in HKEY_CLASSES_ROOT\Directory\shell\Powershell registry key. The command should be changed to powershell.exe -noexit -command "cd -LiteralPath '%V'".

Unfortunately, I have no idea how to override the default command that Windows Explorer runs from Ribbon File menu.

Update 2017-07-31: it looks like there were some changes in Windows 10 Creators Update (1703). It does respect the command from registry key now, so it fixes the issue.

If you can’t see PowerShell item in context menu in Windows 10, even with Shift key pressed, that’s because of ShowBasedOnVelocityId registry key. Microsoft is ready to switch from CMD to PowerShell, including these menu items (it should be switched already in Creators Update (1703)). For now, you can just rename or remove this parameter.

There is also a HideBasedOnVelocityId registry key, that works in opposite way. You may need to deal with it to return CMD option once it turned off.

Since I’m using QtTabBar to extend File Explorer capabilities anyway, I can configure custom command there to run PowerShell in the way I need it.

QtTabBar is not updating for quite a while now, but it works fine with Windows 10. All the panels can be turned on in Ribbon bar, View tab, Options dropdown.

Open QTTabBar options, Application launcher, and add new item like this:

(%C% is replaced by QtTabBar to a non-quoted path for currently opened folder)

As a bonus, you can add another entry for PowerShell v2 (in case you still need to test your modules against it):

Note the -Version parameter.

Unicode

Let’s make one more folder, C:\[test]\Hello, 世界.

Oops! (once again)

It’s 2017 already and yet we can’t use Unicode freely, not only in CMD, but also in PowerShell. At least it’s not falling with some error here, just cannot display paths properly. It’s possible to find some workarounds with code pages, I believe, but they won’t be very convenient.

What I found is that ConsoleZ can show Unicode paths properly even when underlying CMD or PowerShell window can’t. So let’s try to combine solutions and use PowerShell within ConsoleZ:

  1. In ConsoleZ settings add PowerShell tab (set binary path and icon):

  2. In QtTabBar options add new item like this:

    It will open PowerShell tab in ConsoleZ, with additional parameters passed to PowerShell as in previous case.

Now we can work with paths, containing both PowerShell special characters and non-ASCII characters:

Kanji aren’t quite fit into monospaced grid, but still it’s much better than having tofu squares.

And in case you just need a context menu item for ConsoleZ:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\ConsoleZ]
@="Open ConsoleZ PowerShell here"
"Icon"="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
"Extended"=""
"NoWorkingDirectory"=""

[HKEY_CLASSES_ROOT\Directory\shell\ConsoleZ\command]
@="C:\\...\\Console.exe -d %V -t PowerShell -r \"-NoExit -Command cd -LiteralPath '%V'\""

[HKEY_CLASSES_ROOT\Directory\Background\shell\ConsoleZ]
@="Open ConsoleZ PowerShell here"
"Icon"="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
"Extended"=""
"NoWorkingDirectory"=""

[HKEY_CLASSES_ROOT\Directory\Background\shell\ConsoleZ\command]
@="C:\\...\\Console.exe -d %V -t PowerShell -r \"-NoExit -Command cd -LiteralPath '%V'\""

Make sure you provide correct path to Console.exe in both places.

Extended here means it will show up only on Shift+RMB. Remove it to make menu item available all the time.

Background\shell is where commands for current folders context menu are stored.

comments powered by Disqus