PowerShell: How do I create selector on file/folder, whose name is '*' (asterisk/star)?

余生长醉 提交于 2019-12-12 22:38:33

问题


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).


回答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 '*'


来源:https://stackoverflow.com/questions/48786037/powershell-how-do-i-create-selector-on-file-folder-whose-name-is-asterisk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!