Skip to content

Backtick escaping inconsistent #7999

Closed
Closed

Description

@rjmholt

Was investigating escaping a filename for PowerShell in PowerShell/PowerShellEditorServices#765 (comment).

I created a directory with the following files:

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       10/11/2018  10:57 AM              9 [script.ps1
-a----       10/11/2018  10:57 AM              1 `[script.ps1
-a----       10/11/2018  10:57 AM              1 ``[script.ps1
-a----       10/11/2018  10:57 AM              1 ```[script.ps1
-a----       10/11/2018  10:57 AM              1 ````[script.ps1
-a----       10/11/2018  10:57 AM              8 script.ps1

(New-Item doesn't seem to have a LiteralPath parameter by the way)

I then run the following:

foreach ($i in 0..12) { $ticks = '`' * $i; Get-Item "./$ticks[script.ps1" }

Actual Results

The output of this in the given directory gives me:

Get-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: [script.ps1
At line:1 char:44
+ ... h ($i in 0..12) { $ticks = '`' * $i; Get-Item "./$ticks[script.ps1" }
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
+ FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.GetItemCommand



    Directory: C:\Users\roholt\Documents\Dev\sandbox\badnames


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       10/11/2018  10:57 AM              9 [script.ps1
-a----       10/11/2018  10:57 AM              9 [script.ps1
-a----       10/11/2018  10:57 AM              1 `[script.ps1
Get-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: ``[script.ps1
At line:1 char:44
+ ... h ($i in 0..12) { $ticks = '`' * $i; Get-Item "./$ticks[script.ps1" }
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
+ FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.GetItemCommand

-a----       10/11/2018  10:57 AM              1 ``[script.ps1
-a----       10/11/2018  10:57 AM              1 `[script.ps1
-a----       10/11/2018  10:57 AM              1 ```[script.ps1
Get-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: ````[script.ps1
At line:1 char:44
+ ... h ($i in 0..12) { $ticks = '`' * $i; Get-Item "./$ticks[script.ps1" }
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
+ FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.GetItemCommand

-a----       10/11/2018  10:57 AM              1 ````[script.ps1
-a----       10/11/2018  10:57 AM              1 ``[script.ps1
Get-Item : Cannot find path 'C:\Users\roholt\Documents\Dev\sandbox\badnames\`````[script.ps1' because it does not exist.
At line:1 char:44
+ ... h ($i in 0..12) { $ticks = '`' * $i; Get-Item "./$ticks[script.ps1" }
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (C:\Users\roholt...````[script.ps1:String) [Get-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand

Get-Item : Cannot retrieve the dynamic parameters for the cmdlet. The specified wildcard character pattern is not valid: ``````[script.ps1
At line:1 char:44
+ ... h ($i in 0..12) { $ticks = '`' * $i; Get-Item "./$ticks[script.ps1" }
+                                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Get-Item], ParameterBindingException
+ FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.GetItemCommand

More concretely:

> Get-Item './[script.ps1'  # 0
# Error (The specified wildcard character pattern is not valid: [script.ps1)
> Get-Item './`[script.ps1'  # 1
# Finds '[script.ps1'
> Get-Item './``[script.ps1'  # 2
# Finds '[script.ps1'                         <--- ???
> Get-Item './```[script.ps1'  # 3
# Finds '`[script.ps1'
> Get-Item './````[script.ps1'  # 4
# Error (The specified wildcard character pattern is not valid: ``[script.ps1)
> Get-Item './`````[script.ps1'  # 5
# Finds '``[script.ps1'
> Get-Item './``````[script.ps1'  # 6
# Finds '`[script.ps1'                         <--- ???
> Get-Item './```````[script.ps1'  # 7
# Finds '```[script.ps1'
> Get-Item './````````[script.ps1'  # 8
# Error (The specified wildcard character pattern is not valid: ````[script.ps1)
> Get-Item './`````````[script.ps1'  # 9
# Finds '````[script.ps1'
> Get-Item './``````````[script.ps1'  # 10
# Finds '``[script.ps1'                         <--- ???
> Get-Item './```````````[script.ps1'  # 11
# Error (Cannot find path 'C:\Users\roholt\Documents\Dev\sandbox\badnames\`````[script.ps1' because it does not exist.)
> Get-Item './````````````[script.ps1'  # 12
# Error (The specified wildcard character pattern is not valid: ``````[script.ps1)

Cases 0, 1, 3, 4, 5, 7, 8, 9, 11 and 12 make sense. Cases 2, 6 and 10 seem pretty strange to me.

Expected Behaviour

Cases 2, 6 and 10 should report an invalid wildcard pattern, like cases 0, 4, 8 and 12.

Tagging James Truher (@JamesWTruher) because we discussed this offline earlier.

Activity

  1. added
    WG-Engine-Providers
    on Oct 11, 2018
  2. rjmholt commented on Oct 11, 2018

    @rjmholt
    CollaboratorAuthor

    Trying to resolve the PSES issue linked above, I also tried the following:

    • Create files called [script.ps1, `[script.ps1, ``[script.ps1, etc.
    • Populate them with the content 0, 1, 2, etc. respectively (so the content of the file represents the number of backticks in the name).
    • Execute them directly in order

    Here are my results:

    > ./[script.ps1  # 0
    # Error (The specified wildcard character pattern is not valid)
    > ./`[script.ps1  # 1
    # Error (The specified wildcard character pattern is not valid)
    > ./``[script.ps1  # 2
    1
    > ./```[script.ps1 # 3
    1
    > ./````[script.ps1  # 4
    0
    > ./`````[script.ps1  # 5
    0
    > ./``````[script.ps1  # 6
    3
    > ./```````[script.ps1  # 7
    3
    > ./````````[script.ps1  # 8
    # Error (The specified wildcard character pattern is not valid)
    > ./`````````[script.ps1  # 9
    # Error (The specified wildcard character pattern is not valid)s
    > ./``````````[script.ps1  # 10
    # Error (The specified wildcard character pattern is not valid)
    > ./```````````[script.ps1  # 11
    # Error (The specified wildcard character pattern is not valid)
    > ./````````````[script.ps1  # 12
    1
    > ./`````````````[script.ps1  # 13
    1
    > ./``````````````[script.ps1  # 14
    # Error (The specified wildcard character pattern is not valid)
    > ./```````````````[script.ps1  # 15
    # Error (The specified wildcard character pattern is not valid)
    > ./````````````````[script.ps1  # 16
    # Error (The specified wildcard character pattern is not valid)
    > ./`````````````````[script.ps1  # 17
    # Error (The specified wildcard character pattern is not valid)
    > ./``````````````````[script.ps1  # 18
    # Error (The specified wildcard character pattern is not valid)
    > ./```````````````````[script.ps1  # 19
    # Error (The specified wildcard character pattern is not valid)
    C:\Users\roholt\Documents\Dev\sandbox\badnames
    > ./````````````````````[script.ps1  # 20
    2
    # I stopped at this point
    

    I also tried invoking with a string literal:

    > & '.\[script.ps1'  # 0
    # Error (The specified wildcard character pattern is not valid)
    > & '.\`[script.ps1'  # 1
    1
    > & '.\``[script.ps1'  # 2
    0
    > & '.\```[script.ps1'  # 3
    3
    > & '.\````[script.ps1'  # 4
    # Error (The specified wildcard character pattern is not valid)
    > & '.\`````[script.ps1'  # 5
    # Error (The specified wildcard character pattern is not valid)
    > & '.\``````[script.ps1'  # 6
    1
    > & '.\```````[script.ps1'  # 7
    # Error (The specified wildcard character pattern is not valid)
    > & '.\````````[script.ps1'  # 8
    # Error (The specified wildcard character pattern is not valid)
    > & '.\`````````[script.ps1'  # 9
    # Error (The specified wildcard character pattern is not valid)
    > & '.\``````````[script.ps1'  # 10
    2
    > & '.\```````````[script.ps1'  # 11
    # Error (The specified wildcard character pattern is not valid)
    > & '.\````````````[script.ps1'  # 12
    # Error (The specified wildcard character pattern is not valid)
    > & '.\`````````````[script.ps1'  # 13
    # Error (The specified wildcard character pattern is not valid)
    > & '.\``````````````[script.ps1'  # 14
    3
    # I stopped at this point
    
  3. rjmholt commented on Oct 11, 2018

    @rjmholt
    CollaboratorAuthor

    After discussing this further with James Truher (@JamesWTruher), we think one way to get around this is to use the fully provider-qualified name: Microsoft.PowerShell.Core\FileSystem::C:\path\to\`[script.ps1

  4. iSazonov commented on Oct 12, 2018

    @iSazonov
    Collaborator

    Rob Holt (@rjmholt) Please look #7399 - perhaps it could resolve the issue (fix for ContainsValidWildcardPattern).

  5. kwkam commented on Oct 12, 2018

    @kwkam
    Contributor

    I believe this is more related to the WildcardPatten escape & unescape inconsistency mentioned in #7407

  6. mklement0 commented on Jan 9, 2019

    @mklement0
    Contributor

    Re invocation with . / &: methinks the command-name argument should never be interpreted as a wildcard expression (though fixing that would technically be a breaking change) - see #4726

    To add to the cases of unexpected behaviors with -Path: an argument that also contains unescaped wildcard characters introduces the unexpected need for an additional layer of escaping:

    $null = New-Item 'a['
    
    Get-Item 'a`['  # OK - no unescaped metachar.
    
    Get-Item '*`['  # BREAKS - unescaped metachar. *
    
    Get-Item '*``['  # WORKS, but shouldn't be necessary

    Also, interestingly, performing the filtering via -Include does not exhibit the bug:

    Get-ChildItem * -Include '*`['  # OK
  7. rjmholt commented on Jan 9, 2019

    @rjmholt
    CollaboratorAuthor
    Get-Item '*``['  # WORKS, but shouldn't be necessary

    I think this is the most important problem here; the input is unescaped a varying (and hard-to-predict) number of times, which undermines the whole concept of escaping to my mind.

  8. zzJinux commented on Jun 14, 2019

    @zzJinux

    I think I found one more doubtful behavior. Please check if it's right

    Reproduce

    working directory:

    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----        2019-06-14 오후 10:05                []
    d-----        2019-06-14 오후 10:50                ``
    

    Then run the following:

    gi '``[``]'

    Expected

    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----        2019-06-14 오후 10:50                ``
    

    '``[``]' should be interpreted as BACKTICK META-LBRACKET BACKTICK META-RBRACKET, then match ``

    Actual

    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----        2019-06-14 오후 10:05                []
    
  9. mklement0 commented on Sep 6, 2021

    @mklement0
    Contributor

    Rain Sallow (/u/ta11ow) (@vexx32) states that the WG decided that fixing this issue should also encompass a related fix for the native globbing feature - see #15959 (comment)

  10. added
    Area-FileSystem-Provider
    on Nov 30, 2021
  11. added
    Resolution-No Activity
    on Nov 16, 2023
  12. soredake commented on Dec 5, 2023

    @soredake
  13. removed
    Resolution-No Activity
    on Dec 5, 2023
  14. danielzgtg commented on May 2, 2024

    @danielzgtg

    This bug affects Split-Path more severely:

    PS /tmp> Split-Path -Leaf -Resolve -Path './[script.ps1' 
    Split-Path: The specified wildcard character pattern is not valid: [script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './`[script.ps1'
    Split-Path: The specified wildcard character pattern is not valid: [script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './``[script.ps1'
    Split-Path: The specified wildcard character pattern is not valid: [script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './```[script.ps1'
    `[script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './````[script.ps1'
    Split-Path: The specified wildcard character pattern is not valid: ``[script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './`````[script.ps1'
    ``[script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './``````[script.ps1'
    `[script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './```````[script.ps1'
    ```[script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './````````[script.ps1'
    Split-Path: The specified wildcard character pattern is not valid: ````[script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './`````````[script.ps1'
    Split-Path: The specified wildcard character pattern is not valid: ``[script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './``````````[script.ps1'
    ``[script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './```````````[script.ps1'
    Split-Path: Cannot find path '/tmp/`````[script.ps1' because it does not exist.
    PS /tmp> Split-Path -Leaf -Resolve -Path './````````````[script.ps1'
    Split-Path: The specified wildcard character pattern is not valid: ``````[script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './`````````````[script.ps1'
    Split-Path: Cannot find path '/tmp/``````[script.ps1' because it does not exist.
    PS /tmp> Split-Path -Leaf -Resolve -Path './``````````````[script.ps1'
    ```[script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './```````````````[script.ps1'
    Split-Path: Cannot find path '/tmp/```````[script.ps1' because it does not exist.
    PS /tmp> Split-Path -Leaf -Resolve -Path './````````````````[script.ps1'
    Split-Path: The specified wildcard character pattern is not valid: ````````[script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './`````````````````[script.ps1'
    Split-Path: Cannot find path '/tmp/````````[script.ps1' because it does not exist.
    PS /tmp> Split-Path -Leaf -Resolve -Path './``````````````````[script.ps1'
    Split-Path: The specified wildcard character pattern is not valid: ``[script.ps1
    PS /tmp> Split-Path -Leaf -Resolve -Path './```````````````````[script.ps1'
    Split-Path: Cannot find path '/tmp/`````````[script.ps1' because it does not exist.
    PS /tmp> Split-Path -Leaf -Resolve -Path './````````````````````[script.ps1'
    Split-Path: The specified wildcard character pattern is not valid: ``````````[script.ps1
    

    While Get-Item can mention [script.ps1, Split-Path can't even mention it. The real-world use case I have is Split-Path -Leaf -Resolve -Path '/path/to/dir/Title - of video [abc123].webm' , and it's impossible to mention it without -LiteralPath, which has its own problems.

  15. plum7x commented on Oct 20, 2024

    @plum7x

    There are some problem in Start-Process.
    Backtick escaping does not work in -RedirectStandardInput param.

    PowerShell 7.4.5
    PS > Get-ChildItem
    
        Directory: D:\test
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a---            2024/2/7    23:25           5772 a[0].txt
    
    PS > Start-Process `
    >>   -FilePath 'uchardet.exe' `
    >>   -Wait `
    >>   -NoNewWindow `
    >>   -RedirectStandardInput '.\a[0].txt'
    Start-Process: Cannot perform operation because the wildcard path .\a[0].txt did not resolve to a file.
    PS > Start-Process `
    >>   -FilePath 'uchardet.exe' `
    >>   -Wait `
    >>   -NoNewWindow `
    >>   -RedirectStandardInput '.\a`[0`].txt'
    Start-Process: Cannot perform operation because the wildcard path D:\test\a[0].txt did not resolve to a file.

    Backtick escaping works in -RedirectStandardError and -RedirectStandardOutput, but you will get the wrong file name.

    PS > Start-Process `
    >>   -FilePath 'uchardet.exe' `
    >>   -Wait `
    >>   -NoNewWindow `
    >>   -RedirectStandardInput 'a.txt' `
    >>   -RedirectStandardError 'a`[1`].txt' `
    >>   -RedirectStandardOutput 'a`[2`].txt'
    PS > Get-ChildItem
    
        Directory: D:\test
    
    Mode                 LastWriteTime         Length Name
    ----                 -------------         ------ ----
    -a---            2024/2/7    23:25           5772 a.txt
    -a---          2024/10/21    01:33              0 a`[1`].txt
    -a---          2024/10/21    01:33              9 a`[2`].txt
  16. added
    Resolution-No Activity
    on Apr 23, 2025
  17. removed
    Resolution-No Activity
    on Apr 23, 2025
  18. added
    Resolution-No Activity
    on Oct 21, 2025
  19. soredake commented on Oct 21, 2025

    @soredake

    Not stale.

  20. removed
    Resolution-No Activity
    on Oct 21, 2025
  21. added
    Resolution-No Activity
    on Jun 1, 2026
  22. microsoft-github-policy-service commented on Jun 1, 2026

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

  23. 0009-0009-1013-986X commented on Sep 17, 2026

    @0009-0009-1013-986X

    Has this actually been completed?

  24. danielzgtg commented on Sep 17, 2026

    @danielzgtg

    Has this actually been completed?

    Probably not. It was closed for being stale

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-FileSystem-ProviderResolution-No ActivityWG-Engine-Providers

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions