Powershell opening file path with whitespaces

41,000

Solution 1

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.

Solution 2

You can add escaped double quotes so that you pass a quoted argument:

 "`"$filepath`""

Solution 3

I am answering here for a general scenario.

If you need to navigate to a folder for example C:\Program Files from the Powerhsell, the following command won't work as it has white space in between the path.

cd C:\Program Files

Instead embed the path with double quotes as like the below.

cd "C:\Program Files"

Share:
41,000
user3402227
Author by

user3402227

Updated on July 05, 2022

Comments

  • user3402227
    user3402227 almost 2 years

    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?