Is Get-ChildItem -Recurse broken when there are square brackets in the input path?

It looks like a bug reported here or a very similar issue.


TL;DR: Use -LiteralPath for the folder when there might be unusual characters (inc. square brackets) in the names.


I wasn't able to reproduce this as per the OP in PowerShell v5.1.17134.590.

However, I was able to reproduce something similar, using the following command to attempt to list files in a folder I suspected was empty, in order to delete it. In actual fact, this folder had 12 .mp3 files in it:

[PS]> gci '.\Music\Artist - Name\Album Name [Disc 1]\'

[PS]>

Adding the -Recurse switch caused the cmdlet to return an error rather than the (misleading) empty response shown above. I tested the same command with the -Include * option, and still there were no results, however this time there was no error when combined with the -Recurse switch.

-LiteralPath

The thing that worked best for me was to specify the path with the -LiteralPath parameter:

[PS]> gci -LiteralPath '.\Music\Artist - Name\Album Name [Disc 1]\'

Escaping

To cover some other possible cases, you might want to try to escape the square brackets. Using the suggestion from @PetSerAl in the comments on the OP you could try something like this:

[PS]> [System.Management.Automation.WildcardPattern]::Escape('.\Music\Artist - Name\Album Name [Disc 1]\')

.\Music\Artist - Name\Album Name `[Disc 1`]\

Sadly, this didn't work straight away. I found that I needed to escape twice, and then the following command gave the correct directory listing:

[PS]> gci '.\Music\Artist - Name\Album Name ``[Disc 1``]\'

See the following Q&A for more about square brackets & escaping:

  • How do I use square brackets in a wildcard pattern in PowerShell Get-ChildItem?