r/PowerShell • u/Radiant-Photograph46 • Jan 26 '26
Solved Processing special characters in file names
I have some files with brackets in their names and they behave very oddly with powershell. I ran this quick experiment: `@(gci)[0]` in a folder shows one of the files with brackets, then `test-path @(gci)[0]` which… returns False. Big problem here.
How do I fix this behavior? The issue is not with test-path specifically, get-fileHash also returns an empty string, and `test-path @(gci)[0].fullName` also returns False.
3
Upvotes
3
u/Ironic_Jedi Jan 26 '26 edited Jan 26 '26
It's because of the way searches work in powershell. Anything with a square bracket and a number will cause this.
I had this issue with a find and replace string in a file. Was not fun at the time.
Anyway. It's because of regex wildcard search patterns.
For test path check if it has a literalpath parameter
Edit
yeah it does
Use literalpath to skip the regex search issues.
1
u/purplemonkeymad Jan 26 '26
Just so you know pipeline input binds the literalpath parameter first, so for this case you can instead use the pipeline:
1
u/420GB Jan 26 '26
The
-Pathparameter, which is the default, in PowerShell accepts and interprets wildcards. That means things like the star*, question mark?and angle brackets[]are treated as placeholders.Use LiteralPath whenever you don't want wildcard behavior.
-6
u/lescompa Jan 26 '26
MS CoPilot/ChatGPT, etc makes easy work.