Search code examples
powershell

Globbing problem with PowerShell `-Path` cmdlets


I have some files with "meta-characters" in their name, for eg. brackets:

'test[txt].0',  'test[xml].0', 'test[xml].1' | % {New-Item $_} | Out-Null

I can get those files with a glob:

Resolve-Path -Path 'test*'

Path
----
C:\test[txt].0
C:\test[xml].0
C:\test[xml].1

Or a single one of them using backtick-escaping:

Resolve-Path -Path 'test`[xml`].0'

Path
----
C:\test[xml].0

But I can't get the ones that start with test[xml]. using backtick-escaping and a "wildcard":

Resolve-Path -Path 'test`[xml`].*'

What would be the correct "path" to use?


Solution

  • To match files that start with test[xml] using a single glob in PowerShell, you need to escape the brackets properly.

    Resolve-Path -Path 'test``[xml``].*'
    

    In PowerShell, the backtick (`) is used as the escape character. Since you need to escape the brackets, you should use double backticks to escape each bracket.