10

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?

bad_coder
  • 5,829
  • 13
  • 26
  • 41
user3402227
  • 237
  • 2
  • 4
  • 10

5 Answers5

14

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.

CB.
  • 53,323
  • 8
  • 138
  • 146
  • One more question! How do i carry parameters across using this method? I have $courseName i want to pass to the $filePath script which has a courseName parameter. I tried the following but it doesnt work: start-process -FilePath powershell.exe -ArgumentList "-file `"$($filepath.path) `"-courseName $courseName" – user3402227 Apr 04 '14 at 08:23
  • @user3402227 try to post a new question, for me it seem to works, but comments are not ideal to post code.. – CB. Apr 04 '14 at 08:37
12

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

 "`"$filepath`""
mjolinor
  • 59,504
  • 6
  • 99
  • 125
  • Another option would be to use single quotes on the "outside," such that double quotes will be treated as literals. – Trevor Sullivan Apr 03 '14 at 14:39
  • 2
    Then you get the double quotes, but $filepath doesn't expand. It will expand if you put it in single quotes inside of double quotes, but the command line doesn't like the single quotes. – mjolinor Apr 03 '14 at 14:47
  • Hey, Thanks very much for your help. Unfortunately this still doesnt do the trick? Having the $filepath without spaces works, with spaces does not... – user3402227 Apr 03 '14 at 14:56
  • @mjolinor um.. this way the `-file` parameter is for the start-process cmdlet. – CB. Apr 03 '14 at 15:10
  • I would typically do: `'"{0}"' -f $FilePath` – Trevor Sullivan Apr 03 '14 at 15:37
  • 1
    That's another option. Either way, you're going to have to get the file path double quoted in the argument list. – mjolinor Apr 03 '14 at 15:47
2

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"

Sarath KS
  • 15,816
  • 9
  • 67
  • 77
0

File name might contain spaces, so preserve spaces in full path:
Notepad++ exec command:
"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" "& \"$(FULL_CURRENT_PATH)\""

same from command prompt:
"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" "& \"C:\a_work\Systems\name with spaces.ps1\""

Sasha Bond
  • 680
  • 8
  • 8
0

Just in case [string]$shipno (which is path & file name) comes in including spaces the following allows it to be passed to -FilePath successfully:

if ($shipno.contains(" ") -eq $true) {
   $shipno = """" + $shipno + """"
}
JiF
  • 1