Escaping in strings and cmdlets

Brass Contributor

I have observed a pretty weird behaviour when handling file paths containing square brackets - yes, that's rare but it happens. Square brackets are escaped using the backtick, for example


C:\[dir]\subdir => $filePath = "C:\`[dir`]"


That's pretty fine but when you use it with New-Item cmdlet, you'll get in trouble:


New-Item -ItemType Junction -Path "C:\dir1" -Target "C:\``[dir2``]"


You'll need to use two backticks to make it work! Otherwise you'll get an error message saying that the target path is not found even if it exists!

Question is, why is there a difference in escaping? Is there a bug in some cmdlets like New-Item?

1 Reply

@Michael_Buchner Hello Michael, I use the single quote for paths and strings so when I have to scape a char I just use one backtick.


This should work for all cmdlets. 

New-Item -ItemType Junction -Path 'C:\dir1' -Target 'C:\`[dir2`]'



Erick Moreno