Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell opening file path with whitespaces

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?

like image 755
user3402227 Avatar asked Apr 03 '14 14:04

user3402227


People also ask

How do you use file paths with spaces in PowerShell?

In case you want to run powershell.exe -File from the command line, you always have to set paths with spaces in double quotes (""). Also try using the Grave Accent Character (`) PowerShell uses the grave accent (`) character as its escape character. Just add it before each space in the file name.

What does $_ in PowerShell mean?

The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.


1 Answers

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

 "`"$filepath`""
like image 128
mjolinor Avatar answered Oct 15 '22 03:10

mjolinor