How to use Test-Path on a directory containing square brackets in the name

Question:

I have a series of directories that have been created with square brackets in their name. For some inexplicable reason, the Test-Path command returns $false when I run it against these directories, but returns $true if I run the same command against a directory without square brackets in the name.

The directories look like this:

The command I am running is:

I have also tried:

But this also returns $false.

Does anyone know how one might get Test-Path to return true when there really is a directory of that name?

Answer:

You can use the -LiteralPath parameter for this:


Which is probably a better option than adding backticks if you’re processing more than one path:

Source:

How to use Test-Path on a directory containing square brackets in the name by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply