title: How to call function outside a scriptblock
body: <p>I have a function that I should be able to call from any place in my powershell script.  </p>
  
  <p>The problem is that it doesn't identify the function in a script block.  </p>
  
  <p>In the following example I have the function <code>getNumberFive</code> which should return the number 5.<br>
  I want to be able to use this function inside a scriptblock when I start a new job and also in the end of the script.  </p>
  
  <p><strong>Expected result:</strong><br>
  Write the number <strong>15</strong> to the file "C:\tmp\result.txt"<br>
  Write to the console: "I like the number 5"  </p>
  
  <p><strong>In reality:</strong><br>
  Write the number <strong>10</strong> to the file "C:\tmp\result.txt"<br>
  Write to the console: "I like the number 5"  </p>
  
  <p>I can workaround this issue by defining the same function inside the scriptblock but then I will duplicate the function and this is not a good programming.  </p>
  
  <p>Another way is to define:  </p>
  
  <pre><code>$func = {
      function getNumberFive(){
          return 5
      }
  }
  $scriptBlock = {
  
      Function printSum(){
          $number = getNumberFive
          $newNumber = 10 + $number  # =&gt; $newNumber should be 15
          $newNumber &gt;&gt; "C:\tmp\result.txt"
        }
  }
  Start-Job -ScriptBlock $scriptBlock -InitializationScript $func
  </code></pre>
  
  <p>But in this case I won't be able to call <code>$five = getNumberFive</code>.</p>
  
  <p>I read number of methods but I didn't understand how exactly to use them:<br>
  <a href="https://stuart-moore.com/calling-a-powershell-function-in-a-start-job-script-block-when-its-defined-in-the-same-script/" rel="nofollow noreferrer">CALLING A POWERSHELL FUNCTION IN A START-JOB SCRIPT BLOCK WHEN IT’S DEFINED IN THE SAME SCRIPT</a>  </p>
  
  <p><a href="https://stackoverflow.com/questions/15977178/how-to-pass-a-named-function-as-a-parameter-scriptblock">How to pass a named function as a parameter (scriptblock)</a>  </p>
  
  <p><a href="https://social.technet.microsoft.com/Forums/ie/en-US/485df2df-1577-4770-9db9-a9c5627dd04a/how-to-pass-a-function-to-a-scriptblock?forum=winserverpowershell" rel="nofollow noreferrer">https://social.technet.microsoft.com/Forums/ie/en-US/485df2df-1577-4770-9db9-a9c5627dd04a/how-to-pass-a-function-to-a-scriptblock?forum=winserverpowershell</a> </p>
  
  <p><a href="https://stackoverflow.com/questions/22129621/powershell-pass-function-as-a-parameter">PowerShell: Pass function as a parameter</a>  </p>
  
  <p><a href="https://stackoverflow.com/questions/8448808/using-invoke-command-scriptblock-on-a-function-with-arguments">Using Invoke-Command -ScriptBlock on a function with arguments</a>  </p>
  
  <p>My script:  </p>
  
  <pre><code>function getNumberFive(){
      return 5
  }
  
  $scriptBlock = {
  
      Function printSum(){
          # $number = getNumberFive =&gt; DOESN'T WORK
          # $number = Invoke-Expression ($(get-command getNumberFive) | Select -ExpandProperty Definition) =&gt; DOESN'T WORK AS EXPECTED
          # $number = &amp;(${function:getNumberFive}) =&gt; DOESN'T WORK AS EXPECTED
          # $number = &amp;(Get-Item function:getNumberFive) =&gt; DOESN'T WORK AS EXPECTED
          $newNumber = 10 + $number  # =&gt; $newNumber should be 15
          $newNumber &gt;&gt; "C:\tmp\result.txt"
      }
  
      printSum
  }
  
  Start-Job -ScriptBlock $scriptBlock 
  
  $five = getNumberFive
  Write-Host "I like the number"$five
  
  Get-Job | Wait-Job
  Get-Job | Stop-Job
  Get-Job | Remove-Job
  </code></pre>