When you do:
function f1 {
Invoke-Expression "echo $args"
$args
}
you will get:
PS D:\TEMP> f1 --option a 'b c'
--option
a
b
c
--option
a
b c
PS D:\TEMP>
So the use of Invoke-Expression "echo $args"
, does make you get unwanted output....
EDIT: (Because of more info that was given in the question)
When your environment variable also contains parameters which are needed to start your editor, and when you want to pass an unknown number of parameters, you can build the complete statement which is needed to start your editor like this:
function ed {
param(
[Parameter(ValueFromRemainingArguments=$true)][String[]]$Args
)
$cmd = $env:EDITOR + ' ' + [string]::Join(' ',$Args)
& Invoke-Expression $cmd
}
The variable cmd
should contain the complete statement that works when you type it at the prompt.
NOTE: Please take a note of this warning: "Take reasonable precautions when using the Invoke-Expression cmdlet in scripts. When using Invoke-Expression to run a command that the user enters, verify that the command is safe to run before running it. In general, it is best to design your script with predefined input options, rather than allowing freeform input." (see: Invoke-Expression)