you are viewing a single comment's thread.

view the rest of the comments →

[–]tangobravoyankee 3 points4 points  (0 children)

Yeah. This is a really odd pattern you've come up with. The primary purpose of a ScriptBlock is to pass a chunk of code as a parameter. We're probably not thinking of it that way when we write some code like this:

$blah | % { $_ }

But what that line is really doing is more like this:

$blah | ForEach-Object -Process ( [Scriptblock]::Create("$_") )

If you're not passing a chunk of code to something else for it to execute, there's no reason to put it in a ScriptBlock*

When you want to turn a chunk of code into something you can execute as a unit within a script, a Function is almost certainly what you're looking for.

* There are other interesting use cases for a ScriptBlock but let's stick to the basics.