0

Here is a screen shot of the issue. You can see that I'm in a folder named C:\Test[ and I get an error when attempting to run Test.ps1 via a PowerShell command line: enter image description here

Using an absolute file name doesn't make any difference: enter image description here

I also tried escaping the bracket with a backtick, but had no luck with that. enter image description here

Of course, in this simple example, I can just run .\Test.ps1 directly, but this is part of a bigger issue where a PowerShell script is run from an HTA which could be located in a folder that contains a left bracket, right bracket, or both. Different issues occur depending on the combination. It would be too confusing to show all permutations in one question, so I'm starting with this one, as it is the simplest to demonstrate.

My current "solution" is to display an error message if the user is attempting to run the HTA from a directory that contains left or right brackets, but I'd prefer to find a way to make it work.

I've read through various articles regarding square bracket folder names and PowerShell, but they all refer to using "-literalpath" or backticks within a PowerShell script and the only command line reference I found said to use "-file" which, of course, I'm already using.

That was this article: Cannot run PowerShell script in folder with square braces

The answers provided in that article don't work for a folder with a single left bracket and actually don't really solve all issues for folders with a pair of brackets because even though "-file" allows the script to run, there will be further issues if there is a filename parameter passed to the script, but that's another issue. Let's see if we can just sort this single left bracket problem first.

2 Answers 2

1

Looks like that you can use the -InputFormat with the powershell command to help?
The brackets will need double backtick characters within the single quotes, the cat command only needed a single backtick like the cd command:

PS C:\> cat '.\Test`[\test.ps1'

echo "hello"
PS C:\> powershell -InputFormat "text" 'c:\test``[\test.ps1'
hello
Sign up to request clarification or add additional context in comments.

5 Comments

Now for my rant: A workaround for a valid Windows path should not be necessary in a first-class scripting language for Windows. The PowerShell team needs to, at a minimum, make the backtick also work with "-file". But, seriously, they need to figure out how to make brackets not affect file paths at all. Using backtick and "-literalpath" (within the script) totally kills any ability to use relative paths.
Also, there are likely a very large number of PowerShell scripts, currently in production, that will break if they are placed in a folder that has a left bracket, right bracket, or combination of those two characters in the folder name. IMO, the only proper fix is one in which all existing scripts will continue to work, as is, regardless of the folder name.
I definitely understand the rant. I was surprised that kind of handling was needed to workaround the brackets myself. Is there any possible way to fix whatever is causing the brackets to get introduced?
I've decided to stick with my original approach of just throwing an error message. It's just too much trouble to code around square brackets for both the startup of the PowerShell script and any file references within the script. I would have thought that one of the PowerShell requirements is that a script must work, without modification, in any legal Windows folder name. Apparently that's not a concern for the PowerShell team since it's had this issue with brackets for something like 15 years now?
Yeah, too much hell having to deal with [ or ] pairs or only 1 of each.
1

Use ` before a bracket.

For example, I made a folder called hi[, now to go to that directory, use cd 'hi`[' and it will work.

Note: It only works with single quote ('), using double quotes (") throws an error

2 Comments

Though it works with cd, trying to run with the -file option complains about the path with the [ present. Using a ` is not escaping as expected within a single quote. I see the same error that the OP has provided.
The backtick works for CD, but it does not work for the PowerShell command line. I've updated the question to show the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.