title: Calling functions from closures in Powershell
body: <p>I have a function that returns a script block that calls another function. (That may sound overly complicated, but it makes sense in the real code.) </p>
  
  <p>It works in the ISE but not in the regular console.</p>
  
  <p>Am I doing some wrong, or is this a bug in PowerShell? Is there a workaround?</p>
  
  <p>The following is some minimal code that shows the problem:</p>
  
  <pre><code>function SomeFunc([string]$name)
  {
      "Hello, $name!"
  }
  
  function GetBlock([string]$name)
  {
      { SomeFunc $name }.GetNewClosure()
  }
  
  $block = GetBlock("World")
  
  &amp; $block
  </code></pre>
  
  <p><strong>Please put the code in a file</strong> and execute that to see the error. If you just paste it into the console, then there won't be an error.</p>
  
  <p>When I run it in the ISE by pressing F5, I get the expected result:</p>
  
  <pre><code>Hello, World!
  </code></pre>
  
  <p>However, when I run it in the regular console, I get an error:</p>
  
  <pre><code>SomeFunc : The term 'SomeFunc' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  At E:\scratch\Untitled4.ps1:8 char:7
  +     { SomeFunc $name}.GetNewClosure()
  +       ~~~~~~~~
      + CategoryInfo          : ObjectNotFound: (SomeFunc:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
  </code></pre>
  
  <p>This is my <code>$psversiontable</code>:</p>
  
  <pre><code>Name                           Value
  ----                           -----
  PSVersion                      5.1.16299.251
  PSEdition                      Desktop
  PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
  BuildVersion                   10.0.16299.251
  CLRVersion                     4.0.30319.42000
  WSManStackVersion              3.0
  PSRemotingProtocolVersion      2.3
  SerializationVersion           1.1.0.1
  </code></pre>