How to run a powershell script with white spaces i

2019-01-20 02:55发布

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.

Any help would be greatly appreciated!

3条回答
爷的心禁止访问
2楼-- · 2019-01-20 03:30

Try this:

PS C:\> & "C:\Users\test\Documents\test\line space\PS Script\test"
查看更多
beautiful°
3楼-- · 2019-01-20 03:33

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"
)
查看更多
smile是对你的礼貌
4楼-- · 2019-01-20 03:41

-File Parameter

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

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

-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 command line, and & 'C:\Users\test\Documents\Test Space\test.ps1' is 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.

-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
查看更多
登录 后发表回答