Im 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?
Thanks soo much for any help!
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\""
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"
You can add escaped double quotes so that you pass a quoted argument:
Just in case [string]$shipno (which is path & file name) comes in including spaces the following allows it to be passed to -FilePath successfully:
try this:
edit after comments:
side note:
$filepath
is a[pathinfo]
type and not a[string]
type.