Powershell opening file path with whitespaces

user3402227 picture user3402227 · Apr 3, 2014 · Viewed 39.4k times · Source

I'm my PS script I want to be able to run another script in another PS instance by doing the following:

$filepath = Resolve-Path "destruct.ps1"
    
start-process powershell.exe "$filepath"

destruct.ps1 is in the same folder as this script.

However when running this script in a location which includes spaces ("C:\My Scripts\") I will get the following error:

The term 'C:\My' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.

I know by using a '&' with the Invoke-Expression method solves this problem, how can I do the same but by using the start-process method?

Answer

CB. picture CB. · Apr 3, 2014

try this:

 start-process -FilePath powershell.exe -ArgumentList "-file `"$filepath`""

edit after comments:

start-process -FilePath powershell.exe -ArgumentList "-file `"$($filepath.path)`""

side note:

$filepath is a [pathinfo] type and not a [string] type.