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.

For PowerShell cmdlets, can I always pass a script block to a string parameter?

# Delay-bind script-block argument: # The code inside { … } is executed for each input object ($_) and # the output is passed to the -NewName parameter. … | Rename-Item -NewName { $_.Name -replace ‘\.txt$’,’.log’ } The call above shows an application of a delay-bind script-block ({ … }) argument, which is an implicit … Read more