You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Wildcard character in registry paths results in "Illegal characters in path." #4704
>$PSVersionTable
Name Value
---------
PSVersion 6.0.0-beta
PSEdition Core
GitCommitId v6.0.0-beta.6
OS Microsoft Windows 10.0.16275
Platform Win32NT
PSCompatibleVersions {1.0,2.0,3.0,4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Positionally, your argument binds to -Path, which performs wildcard matching, which would delete the shell subkeys of all subkeys of HKCU:\Software\Classes.
Also, there is no escaping of wildcard expressions; "HKCU:\Software\Classes\``*\shell\" tries to match HKCU:\Software\Classes\`*\shell\ as a pattern, which is not the intent.
The only way to suppress wildcard matching is to use -LiteralPath.
A test case to demonstrate the problem succinctly:
# Note that paths passed to New-Item are *always* interpreted as literals paths.# (Its -Path parameter is the equivalent of the -LiteralPath parameter in the Get-* and Remove-* cmdlets.)New-Item-Force HKCU:\_tmp\*\sub, HKCU:\_tmp\foo\sub
# -LiteralPath: Should only delete the key with the *literal* "*" in its path.Remove-Item-LiteralPath HKCU:\_tmp\*\sub\
# -Path (implied by positional use: should apply wildcard matching and delete# the "sub" subkeys of ALL "HKCU:\_tmp" subkeys.Remove-Item HKCU:\_tmp\*\sub\
Remove-Item-Recurse HKCU:\_tmp
Currently, the first 2 Remove-Item calls fail with Illegal characters in path.
Note that the commands fail on Windows PowerShell too, but quietly.
Thank you very much for quick response and for the tip on -LiteralPath, I was unaware of this option.
One other note on where I got HKCU:\Software\Classes\``*\shell\ from: as part of my debugging, I was trying to cd into the path with the "*" key in it. It seems that cddoes expand the wildcard. I was able to prevent the expansion of the wildcard and cd into the path with no problem when I escaped the backtick. I assumed this was because it might be getting passed to some code downstream that turns the backtick into an actual escape sequence. I have no idea if this is related, but perhaps it gives a little more info on the bug.
With unescaped wildcard:
cd "HKCU:\Software\Classes\*\shell\"
cd : Cannot set the location because path 'HKCU:\Software\Classes\*\shell\' resolved to multiple containers. You can only the set location to a
single container at a time.
At line:1 char:1+ cd "HKCU:\Software\Classes\*\shell\"+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Location], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.SetLocationCommand
With escaped backtick:
cd "HKCU:\Software\Classes\``*\shell\"
pwd
Path
----
HKCU:\Software\Classes\*\shell\
I still puzzled by the escaping with two` (effective) instances, however, given the following:
[System.Management.Automation.WildcardPattern]::Unescape('``*')
`*# !! 1 ` retained, as expected.
Given that it's easy to get the escaping wrong manually and it is cumbersome to apply it programmatically, escaping is best reserved for cases where you have a wildcard expression in which you must escape parts as literals; in other words: if you know that your path is meant to be taken literally, -LiteralPath is the simple and robust choice.
Activity
mklement0 commentedon Aug 30, 2017
You did hit a bug, but there are two important asides:
You should always use
-LiteralPathto prevent (potentially unwanted) wildcard matching:Positionally, your argument binds to
-Path, which performs wildcard matching, which would delete theshellsubkeys of all subkeys ofHKCU:\Software\Classes.Also, there is no escaping of wildcard expressions;"HKCU:\Software\Classes\``*\shell\"tries to matchHKCU:\Software\Classes\`*\shell\as a pattern, which is not the intent.The only way to suppress wildcard matching is to use.-LiteralPathmklement0 commentedon Aug 30, 2017
A test case to demonstrate the problem succinctly:
Currently, the first 2
Remove-Itemcalls fail withIllegal characters in path.Note that the commands fail on Windows PowerShell too, but quietly.
jared-is-ray commentedon Aug 30, 2017
Thank you very much for quick response and for the tip on -LiteralPath, I was unaware of this option.
One other note on where I got
HKCU:\Software\Classes\``*\shell\from: as part of my debugging, I was trying tocdinto the path with the "*" key in it. It seems thatcddoes expand the wildcard. I was able to prevent the expansion of the wildcard andcdinto the path with no problem when I escaped the backtick. I assumed this was because it might be getting passed to some code downstream that turns the backtick into an actual escape sequence. I have no idea if this is related, but perhaps it gives a little more info on the bug.With unescaped wildcard:
With escaped backtick:
mklement0 commentedon Aug 30, 2017
Jared Ray (@jared-is-ray):
The escaping trick is intriguing -
I would not expect that to work, and, bizarrely, it even works with two backticks (but no more):Again, however,
-LiteralPathis the right solution:mklement0 commentedon Sep 1, 2017
Jared Ray (@jared-is-ray):
My bad: You can escape wildcard patterns, and what you tried should have worked, after all.
`(effective) instances, however, given the following:-LiteralPathis the simple and robust choice.