Skip to content

Wildcard character in registry paths results in "Illegal characters in path." #4704

Closed

Description

In Win10 (10.0.16275.1000) and beta6, I am unable to use powershell to modify registry keys that are named "*".

Steps to reproduce

Remove-Item "HKCU:\Software\Classes\*\shell\"
# or try with escape
Remove-Item "HKCU:\Software\Classes\``*\shell\"

Expected behavior

Registry key is removed.

Actual behavior

Remove-Item : Illegal characters in path.
Parameter name: path
At line:1 char:1
+ Remove-Item "HKCU:\Software\Classes\``*\shell\"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Remove-Item], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.RemoveItemCommand

Environment data

> $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

Activity

  1. added
    WG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Aug 30, 2017
  2. mklement0 commented on Aug 30, 2017

    @mklement0
    Contributor

    You did hit a bug, but there are two important asides:

    You should always use -LiteralPath to prevent (potentially unwanted) wildcard matching:

    Remove-Item -LiteralPath "HKCU:\Software\Classes\*\shell\"

    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.

  3. mklement0 commented on Aug 30, 2017

    @mklement0
    Contributor

    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.

  4. jared-is-ray commented on Aug 30, 2017

    @jared-is-ray
    Author

    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 cd does 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\
  5. mklement0 commented on Aug 30, 2017

    @mklement0
    Contributor

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

    # Inexplicably works even with *2* (effective) backticks.
    cd "HKCU:\Software\Classes\````*\shell\" # same as: 'HKCU:\Software\Classes\``*\shell\'

    Again, however, -LiteralPath is the right solution:

    cd -LiteralPath HKCU:\Software\Classes\*\shell\
  6. mklement0 commented on Sep 1, 2017

    @mklement0
    Contributor

    Jared Ray (@jared-is-ray):

    My bad: You can escape wildcard patterns, and what you tried should have worked, after all.

    > [System.Management.Automation.WildcardPattern]::Escape('HKCU:\Software\Classes\*\shell\')
    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.
  7. added
    Issue-BugIssue has been identified as a bug in the product
    and removed
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Sep 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions