1

I have a PowerShell script in a folder with square braces, for example:

When I try and run my script from the command line using:

C:\Temp\[test]>Powershell.exe .\MyScript.ps1

I get the following output:

enter image description here

If I put the same script in a folder without square braces and run:

C:\Temp\(test)>Powershell.exe .\MyScript.ps1

It works and I get the following output:

enter image description here

The script is part of an automation project and must be in a folder with square braces. Any help would be greatly appreciated.

1 Answer 1

3

Problem: passing a local path to powershell.exe that includes square brackets in the folder names means that powershell cannot find the script file passed to it.

Option 1: use powershell -File .\MyScript.ps1
Option 2: use powershell %CD%\MyScript.ps1

I am not clear why powershell cannot find your path, but the -File command line argument seems to fix it. As does using the %CD% batch file property instead of '.'

If I create a file "HelloWorld.ps1" containing the single line:

Write-Output "Hello World"

in a folder

c:\work\joel\scream

and run the command:

C:\work\joel\scream>powershell .\HelloWorld.ps1

then I get the expected output (Hello World).

If I rename the folder to [scream], it fails.

cd ..
ren scream [scream]

Square brackets is a range operator.

C:\work\joel\[scream]>powershell .\HelloWorld.ps1

now yields:

.\helloworld.ps1 : The term '.\helloworld.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

However running:

C:\work\joel\[scream]>powershell -File .\HelloWorld.ps1   

gives the expected output (Hello World).

powershell /?
<snip/>
-File
    Runs the specified script in the local scope ("dot-sourced"), so that the
    functions and variables that the script creates are available in the
    current session. Enter the script file path and any parameters.
    File must be the last parameter in the command, because all characters
    typed after the File parameter name are interpreted
    as the script file path followed by the script parameters.

I'm not clear whether powershell or the command prompt / batch script is misinterpreting the path but I can say that while:

C:\work\joel\[scream]>powershell .\HelloWorld.ps1

does not work:

C:\work\joel\[scream]>powershell %CD%\helloworld.ps1 

does work. so it appears to be related to the expansion of '.' into the current path.

Sign up to request clarification or add additional context in comments.

3 Comments

He is not using the square brackets in his command - there seem to be nothing to escape. It's the current path that contains square brackets.
Ok @zespri, I'll pay that, it isn't to do with powershell's usage of square brackets for ranges.
@zespri Understood that the brackets are in the path. However, the . expands to the current working directory, hence there are brackets in the command, indirectly. The correct answer is in the middle of his answer - use powershell.exe -File .\MyScript.ps1. I'm not sure, though, why powershell -Command ".\MyScript.ps1" doesn't work when .\MyScript.ps1 works if typed at the prompt in a directory with brackets. I noticed that tab expansion also doesn't work in directories with brackets.

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.