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?
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.
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.
You can add escaped double quotes so that you pass a quoted argument:
"`"$filepath`""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With