title: How do I include a locally defined function when using PowerShell's Invoke-Command for remoting?
body: <p>I feel like I'm missing something that should be obvious, but I just can't figure out how to do this.</p>
  
  <p>I have a ps1 script that has a function defined in it. It calls the function and then tries using it remotely:</p>
  
  <pre><code>function foo
  {
      Param([string]$x)
  
      Write-Output $x
  }
  
  foo "Hi!"
  
  Invoke-Command -ScriptBlock { foo "Bye!" } -ComputerName someserver.example.com -Credential someuser@example.com
  </code></pre>
  
  <p>This short example script prints "Hi!" and then crashes saying "The term 'foo' is not recognized as the name of a cmdlet, function, script file, or operable program."</p>
  
  <p>I understand that the function is not defined on the remote server because it is not in the ScriptBlock. I could redefine it there, but I'd rather not. I'd like to define the function once and use it either locally or remotely. Is there a good way to do this?</p>