39

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Awsmike
  • 751
  • 1
  • 7
  • 19

5 Answers5

51

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vrdse
  • 2,899
  • 10
  • 20
  • 2
    Nicely done; as an aside: in PowerShell _Core_, `-File` is now the default (this change was necessary to support Unix shebang lines). – mklement0 Mar 05 '18 at 13:45
  • 2
    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. – Tyler Nielsen Jul 19 '18 at 19:02
31

Try this:

& "C:\Users\test\Documents\test\line space\PS Script\test"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
C.Hennessey
  • 329
  • 2
  • 2
  • 4
    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. – Maximilian Burszley Aug 18 '17 at 15:56
  • 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. – cghore Feb 07 '20 at 19:14
  • ah... ampersand...Exactly what I was looking for – raddevus Jan 23 '23 at 22:26
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"
)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • 1
    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 Aug 18 '17 at 16:05
  • @Awsmike Try removing the single-quotes and see what happens – Maximilian Burszley Aug 18 '17 at 16:07
  • 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. – Awsmike Aug 18 '17 at 16:09
  • Alright, so it is interpreting the spaces as extra arguments to the file. Is the path valid in the first place? @Awsmike – Maximilian Burszley Aug 18 '17 at 16:10
  • Your original answer with double quotes instead of single quotes actually worked! – Awsmike Aug 18 '17 at 16:29
0

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 :-)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kbisang
  • 558
  • 4
  • 13
0

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brian
  • 238
  • 1
  • 4