Functions in Powershell - can they be called within a string? Functions in Powershell - can they be called within a string? powershell powershell

Functions in Powershell - can they be called within a string?


Wrap the command/function call in a subexpression inside an expandable string:

"The results of the test are: $(val "yo!")"

Also worth pointing out, the syntax for command invocation in PowerShell doesn't require parentheses. I would discourage using parentheses like you do in the example altogether, since you'll end up in situations where consecutive arguments are being treated as one:

function val ($inputOne,$inputTwo){    "One: $inputOne"    "Two: $inputTwo"}

Now, using C#-like syntax, you'd do:

val("first","second")

but find that the output becomes:

One: first secondTwo: 

because the PowerShell parser sees the nested expression ("first","second") and treats it as a single argument.

The correct syntax for positional parameter arguments would be:

val "first" "second"