Pass arguments to a scriptblock in powershell

Keith’s answer also works for Invoke-Command, with the limit that you can’t use named parameters. The arguments should be set using the -ArgumentList parameter and should be comma separated.

$sb = {
    param($p1,$p2)
    $OFS=','
    "p1 is $p1, p2 is $p2, rest of args: $args"
}
Invoke-Command $sb -ArgumentList 1,2,3,4

Also see here and here.

Leave a Comment