'Spaces in path throwing error when using ArgumentList parameter of Start-Process [duplicate]

In my profile I've got the following function:

function Start-VS {  
  param ([string]$sln, [switch]$UseNineteen )

  if ($UseNineteen) {
    $vs = "c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.exe"
    $vsWorkDir = "c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\"
  }
  else {
    $vs = "C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\devenv.exe"
    $vsWorkDir = "C:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\"  
  }
  
  if (-Not ([string]::IsNullOrEmpty($sln))) {
    $TestP = (Test-Path $sln)
    if ( $TestP -eq $True) {
      $FullPath = Convert-Path $sln
      Start-Process $vs -WorkingDirectory $vsWorkDir -ArgumentList $FullPath  
    }
    else {
      Write-Output $FullPath
      Write-Output "Please use a correct pathway to a solution file" 
    }
  }
  else {
    Start-Process $vs -WorkingDirectory $vsWorkDir 
  }
}

To call it from the PS prompt I'm doing the following:

start-vs -sln "C:\demos\m02 arrays\Example.sln"

Visual studio opens but complains that it cannot find the files - I understand it is because of the space in m02 arrays - how do I alter the function so that it can handle spaces?



Solution 1:[1]

Change

Start-Process $vs -WorkingDirectory $vsWorkDir -ArgumentList $FullPath

to

Start-Process $vs -WorkingDirectory $vsWorkDir -ArgumentList "`"$FullPath`""

Notice the escaped "s surrounding $FullPath. This will include the surrounding quotes to be sent as part of the argument which is needed for paths (or other strings) that include spaces

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel