2

I have a need to modify registry from PS. This registry item is related to context menu for particular files (folder for all extensions).

\\HKEY_CURRENT_USER\Software\Classes\*

Currently I'd like to add items to this path:

HKCU:\Software\classes\*
|- shell
|- shellex

However when I do dir on this path, there is output of all items in classes folder:

PS D:\> dir "HKCU:\Software\classes\*"   

    Hive: HKEY_CURRENT_USER\Software\classes    

Name                           Property
----                           --------
*
.3g2                           VLC.backup : WMP11.AssocFile.3G2
                               (default)  : VLC.3g2
.3gp                           VLC.backup : WMP11.AssocFile.3GP
                               (default)  : VLC.3gp
.3gp2                          VLC.backup : WMP11.AssocFile.3G2
                               (default)  : VLC.3gp2
.3gpp                          VLC.backup : WMP11.AssocFile.3GP
                               (default)  : VLC.3gpp
.3mf

If I understand it correctly, my query is being wildcarded by asterisk. The registry item (folder) is named *. How do I create selector just for this folder and not make it work as a wild-card? Also should work for all other commands, not just the dir (Get-ChildItem).

Already tried these with no success:

PS D:\> dir "HKCU:\Software\classes\**"
PS D:\> dir "HKCU:\Software\classes\\*"
PS D:\> dir "HKCU:\Software\classes\'*'"
PS D:\> dir "HKCU:\Software\classes\`*"

Workaround for Get-ChildItem:

# argument -LiteralPath, unfortunatelly only for "dir" command
PS D:\> dir -LiteralPath "HKCU:\Software\classes\*"

Edit: As I have received answers related particulary to Get-ChildItem, please assume that same path should work for New-Item (which doesn't support -LiteralPath).

Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
  • Have you tried escaping the `*` using a grave accent **`**? so it's be: "HKCU:\Software\classes\`*" – G42 Feb 14 '18 at 11:39
  • @gms0ulman same output as the symbol is not there – Tatranskymedved Feb 14 '18 at 11:40
  • 2
    Have you used `-LiteralPath` to escape the `*`? dir -LiteralPath "HKCU:\Software\classes\*" – Vivek Kumar Singh Feb 14 '18 at 11:42
  • @VivekKumar Nice one - my next suggestion! Bear in mind this might not work with the `dir` alias; you may have to use the native name `Get-ChildItem` – G42 Feb 14 '18 at 11:42
  • @VivekKumar This is great suggestion, unfortunatelly this should not be related to dir command only. e.g. I want to use `New-Item`, which is not supporting arg `-LiteralPath`. Anyway thanks for suggestion as it works for dir. =) – Tatranskymedved Feb 14 '18 at 11:44
  • 1
    The parameter `-LiteralPath` is supported by `dir` cmdlet which is an alias for `Get-ChildItem`. – Vivek Kumar Singh Feb 14 '18 at 11:46

2 Answers2

1
Get-ChildItem -LiteralPath "HKCU:\Software\classes\*"

Or:

Get-ChildItem "HKCU:\Software\classes\" | Where-Object PSChildName -eq '*' | Get-ChildItem

If you want a general solution for the object itself:

Get-ChildItem "HKCU:\Software\classes\" | Where-Object PSChildName -eq '*'
Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
  • The first option was described before in comments/question, but is related to `Get-ChildItem` only. The second option is actually not working as it tries to read from `HKEY_CURRENT_USER\Software\classes` instead of `classes\*` folder. – Tatranskymedved Feb 15 '18 at 08:02
  • @Tatranskymedved Updated. Assumed that "selector for just this folder" meant you wanted the object, not it's children. – Bacon Bits Feb 15 '18 at 13:06
  • I'm sorry BB, but as I mentioned in this question this is not related to the 'Get-ChildItem' only, but also other commands. Hopefully this should go with some symbol in string path, otherwise I canot imagine that MS or other developers work with this registry item. Thanks for the answer anyway. – Tatranskymedved Feb 15 '18 at 14:35
  • @Tatranskymedved If you're looking for a general solution for commands unrelated to `Get-ChildItem`, then `-LiteralPath` isn't a workaround, it's the best answer. Most MS commands that have a `-Path` option also have a `-LiteralPath` parameter precisely for this reason. If a command doesn't support `-LiteralPath` and applies wildcard rules to strings that are passed to it, there is *no* general mechanism to escape those wildcards. – Bacon Bits Feb 15 '18 at 16:51
  • Hey, based on last comment, you might want to see answer from jdgregson ( https://stackoverflow.com/a/68642758/7167572 ). It appears that there is escaping that works. – Tatranskymedved Aug 11 '21 at 18:11
1

PowerShell 5

In PowerShell 5 you can get this to work by using backticks inside of single quotes, and then specifying a directory or an unescaped * after the path. For your example, you can do:

dir 'HKCU:\Software\classes\`*\*'

To create an item, you can use the following syntax:

New-Item -Path 'HKCU:\SOFTWARE\Classes\`*\shell\' -Name "My New Item"

PowerShell 7 (6?)

In PowerShell 7 (and possibly 6) you can escape the asterisk with two backticks (``) inside of double quotes. For your example, you can do:

dir "HKCU:\Software\classes\``*"

To create an item, you can use the following syntax:

New-Item -Path "HKCU:\SOFTWARE\Classes\``*\shell" -Name "My New Item"

Note that the PowerShell 5 method still works in PowerShell 7, so it might be a good idea to use that for backward and forward compatibility.

jdgregson
  • 1,457
  • 17
  • 39