title: Why do I need to have my functions written first in my PowerShell script?
body: <p>I have a script that I am utilizing functions to wrap parts of the code that allow me to move through the sections at a specified point.  What I have found is that I have to have the functions listed first in the script for it to run correctly.</p>
  
  <h1>Non-working example</h1>
  
  <pre><code>$stepChoice = read-host 'Where would you like to start.'
  
  switch($stepChoice)
  {
      1{Step1}
      2{Step2}
      3{Step3}
  
  }
  
  # Steps.ps1 
  function Step1 { 
      'Step 1' 
      Step2 
  } 
  function Step2 { 
      'Step 2' 
      Step3 
  } 
  function Step3 { 
      'Step 3' 
      'Done!' 
  }
  </code></pre>
  
  <h2>Error</h2>
  
  <p>This give me the following error:<br></p>
  
  <blockquote>
    <p>The term 'Step1' 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.  </p>
  
  <pre><code> At C:\Tools\Scripts\functiontest.ps1:7 char:12
    +     1{Step1 &lt;&lt;&lt;&lt; }
    + CategoryInfo          : ObjectNotFound: (Step1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException*
  </code></pre>
  </blockquote>
  
  <h1>Working example</h1>
  
  <p>If I change the order around it works fine:</p>
  
  <pre><code># Steps.ps1 
  function Step1 { 
      'Step 1' 
      Step2 
  } 
  function Step2 { 
      'Step 2' 
      Step3 
  } 
  function Step3 { 
      'Step 3' 
      'Done!' 
  }
  
  #steps
  $stepChoice = read-host 'Where would you like to start.'
  
  switch($stepChoice)
  {
      1{Step1}
      2{Step2}
      3{Step3}
  
  }
  </code></pre>
  
  <h1>Why?</h1>
  
  <p>I am guessing that it is because PS is not loading the functions.</p>
  
  <p>Why is this and is there a better way to lay out this code structure?</p>