I'm trying something really simple here, but can't figure out where I'm going wrong. I've found many other useful discussions of this - particularly here - but haven't found anything that covers my specific scenario.
In powershell, I have typed the following:
$path = "c:\program files\"
$path2 = "c:\program files\fred2\"
echoargs $path $path2
echoargs "$path" "$path2"
In both calls to echoargs, I get
Arg 0 is <c:\program files" c:\program>
Arg 1 is <files\fred2">
back as the result. How can I get the parameters to be passed through correctly?
NB: In my real script the path variables are built up from a few config parameters, so I can't just pass them directly in single quotes.
Use quotation marks when specifying long filenames or paths with spaces. For example, typing the copy c:\my file name d:\my new file name command at the command prompt results in the following error message: The system cannot find the file specified.
In case you want to run powershell.exe -File from the command line, you always have to set paths with spaces in double quotes ("").
To display the value of a variable, type the variable name, preceded by a dollar sign ( $ ). To change the value of a variable, assign a new value to the variable. The following examples display the value of the $MyVariable variable, changes the value of the variable, and then displays the new value.
You need to enclose your result strings in single quotes inside the scope of the execution:
echoargs "'$path'" "'$path2'"
This will pass them to the called application delimited inside single quotes, but since the entire string is still in double quotes your parameter will be expanded correctly.
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