How to run a powershell script with white spaces in path from command line?

后端 未结 5 1266
既然无缘
既然无缘 2020-12-03 10:36

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:



        
相关标签:
5条回答
  • 2020-12-03 11:21

    Try this:

    PS C:\> & "C:\Users\test\Documents\test\line space\PS Script\test"
    
    0 讨论(0)
  • 2020-12-03 11:30

    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"
    )
    
    0 讨论(0)
  • 2020-12-03 11:31

    In case you use parameters you can do as follow

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

    Thanks at this point to Hesham A. Amin :-)

    0 讨论(0)
  • 2020-12-03 11:31

    To add to this topic:

    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 powershell.

    Batch file:

    pushd O:\Data\QuickList
    start powershell -noexit -Command ".\QuickList.ps1 -datafile '%1'"
    popd
    
    0 讨论(0)
  • 2020-12-03 11:42

    -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
    
    0 讨论(0)
提交回复
热议问题