I have a variable that stores a switch statement
$com = '
switch ($_)
{
1 {"It is one."}
2 {"It is two."}
3 {"It is three."}
4 {"It is four."}
}
'
I am trying to pipe in the number to run the switch statement
something like:
1 | iex($com)
Your options are:
scriptblockorfunctionwith aprocessblock:filter, exactly the same functionality:ScriptBlock.Createmethod (this would require aprocessblock in the string expression):ScriptBlock.InvokeWithContextmethod and the automatic variable$input, this technique does not stream and also requires an outerscriptblockto work, it's just to showcase and should be discarded as an option:Invoke-Expression, also requires an outerscriptblockwith aprocessblock (should be discarded - from all techniques displayed above this is the worst one, the string expression is being evaluated per item passed through the pipeline):