Search code examples
powershellcmdscripting

How can I run a PowerShell script with white spaces in the path from the command line?


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?


Solution

  • 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"
    )