54

So I've tried a bunch of different ways to run a PowerShell script from the command line and every single one returns an error.

Here is this path:

C:\Users\test\Documents\test\line space\PS Script\test.ps1

I've tried these:

powershell -File '"C:\Users\test\Documents\test\line space\PS Script\test.ps1"'

powershell "& ""C:\Users\test\Documents\test\line space\PS Script\test.ps1"""

Powershell "& 'C:\Users\test\Documents\test\line space\PS Script\test.ps1'"

Powershell -File 'C:\Users\test\Documents\test\line space\PS Script\test.ps1'"

I get all these errors:

& : The term 'C:\Users\test\Documents\test\line space\PS Script' 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.

Processing -File ''C:\Users\test\Documents\test\line space\PS Script'' failed: The given path's format is not support ed. Specify a valid path for the -File parameter.

How can I fix this?

1
  • 1
    Why are you running it from cmd? Commented Aug 18, 2017 at 15:48

5 Answers 5

60

The -File parameter

If you want to run powershell.exe -File from the command line, you always have to set paths with spaces in double quotes ("). Single quotes (') are only recognized by PowerShell. But as powershell.exe is invoked (and hence the file parameter processed) by the command line, you have to use ".

powershell.exe -File "C:\Users\test\Documents\Test Space\test.ps1" -ExecutionPolicy Bypass

The -Command parameter

If you use the -Command parameter, instead of -File, the -Command content is processed by PowerShell. Hence you can - and in this case have to - use ' inside ".

powershell.exe -Command "& 'C:\Users\test\Documents\Test Space\test.ps1'" -ExecutionPolicy Bypass

The double quotes are processed by the command line, and & 'C:\Users\test\Documents\Test Space\test.ps1' is a command that is actually processed by PowerShell.

Solution 1 is obviously simpler.

Note that -Command is also the default parameter that is used, if you do not specify any.

powershell.exe "& 'C:\Users\test\Documents\Test Space\test.ps1'" -ExecutionPolicy Bypass

This would work, too.

The -EncodedCommand parameter

You can encode your command as Base64. This solves many "quoting" issues and is sometimes (but not in your case though) the only possible way.

First you have to create the encoded command

$Command = "& 'C:\Users\test\Documents\Test Space\test.ps1'"
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Command))

And then you can use the the -EncodedCommand parameter like this

powershell.exe -EncodedCommand JgAgACcAQwA6AFwAVQBzAGUAcgBzAFwAdABlAHMAdABcAEQAbwBjAHUAbQBlAG4AdABzAFwAVABlAHMAdAAgAFMAcABhAGMAZQBcAHQAZQBzAHQALgBwAHMAMQAnAA== -ExecutionPolicy Bypass
Sign up to request clarification or add additional context in comments.

2 Comments

Nicely done; as an aside: in PowerShell Core, -File is now the default (this change was necessary to support Unix shebang lines).
I feel like this is a better answer than the accepted answer. Just putting file path in quotes using -file parameter didn't work at all for me.
42

Try this:

& "C:\Users\test\Documents\test\line space\PS Script\test"

5 Comments

He's trying to use cmd, so I'm assuming he's invoking it from a .cmd or .bat file. The PS Invoke (&) operator won't do any good for him.
PS C:\> & "C:\Temp\Folder with Whitespaces\SomeApp.exe" /switchA valA /switchB valB will work in PowerShell if the ampersand is used as prefix. Without the ampersand, it will error out. For a DOS console, the ampersand is not required. I think this is where C.Hennessey was going.
ah... ampersand...Exactly what I was looking for 👍🏽
This question shows up on google even when searching for how to run a ps script in powershell, that's why this answer is relevant (for those wondering)
Sometimes you need to run programs on the command line with parameters, and this approach work.
8

In your examples, you're mixing quotes and double quoting for no reason.

IF EXIST "C:\Users\test\Documents\test\line space\PS Script\test.ps1" (
  powershell -ExecutionPolicy Unrestricted -File "C:\Users\test\Documents\test\line space\PS Script\test.ps1"
)

5 Comments

This didn't work for me, the script didn't trigger. I tried running just this part: powershell -ExecutionPolicy Unrestricted -File 'C:\Users\test\Documents\test\line space\PS Script\test.ps1' and got this error: The given path's format is not supported.
@Awsmike Try removing the single-quotes and see what happens
Processing -File C:\Users\test\Documents\test\line space\PS Script\test.ps1 failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again.
Alright, so it is interpreting the spaces as extra arguments to the file. Is the path valid in the first place? @Awsmike
Your original answer with double quotes instead of single quotes actually worked!
1

I needed to pass a parameter with spaces.

I am dragging and dropping a file onto a batch file, and the file is off on the server with spaces in the path and/or file name. After testing the above answers, I got this to work. Note I am changing to the working directory prior to starting the PowerShell executable.

Batch file:

pushd O:\Data\QuickList
start powershell -noexit -Command ".\QuickList.ps1 -datafile '%1'"
popd

Comments

-1

In case you use parameters you can do as follows.

powershell.exe -command "& {&'C:\A B C\foo.ps1' param1 param2}"

Thanks at this point to a blog post by Hesham A. Amin :-)

2 Comments

Note that there's no reason to use "& { ... }" in order to invoke code passed to PowerShell's CLI via the -Command (-c) parameter - just use "..." directly. Older versions of the CLI documentation erroneously suggested that & { ... } is required, but this has since been corrected.
Aside from that, unless you need to pass arguments whose values must be interpreted by PowerShell rather than representing (potentially up-front expanded) literal values, using -File rather than -Command is the better choice, as noted in previous answers, not least because only -File passes a script's exit code through as-is.

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.