Running a Script Block with Parameters

by Jun 18, 2012

Maybe you want to design a script that takes code as user input. How would you run that submitted piece of code and pass parameters to it?

To run a submitted script block, use Invoke-Command. This cmdlet can run script blocks locally and remote, and it has a built-in mechanism to supply parameters to the script block.

$code = { param($name) Get-EventLog -LogName $name -EntryType Error }

Invoke-Command -ScriptBlock $code -ArgumentList System 

Twitter This Tip! ReTweet this Tip!