title: Invoke-Command with local function in ScriptBlock doesn't work when the function has no return statement
body: <p>I'm trying to get the <code>Invoke-Command</code> cmdlet working with a CredSsp session by passing a locally defined function.</p>
  
  <p>I have two local functions:</p>
  
  <ul>
  <li><code>MyCopy</code> which just invokes Copy-Item cmdlet on passed params with additional switches</li>
  <li><code>MyCopyReturn</code> which is the same as above, but in addition has <code>return</code> statement at the end</li>
  </ul>
  
  <p>Now when I pass those functions to <code>Invoke-Command</code> without <code>-Session</code> parameter, they both succeed.
  However, when specifying <code>-Session</code> for <code>Invoke-Command</code>, <code>MyCopy</code> fails with following exception (2. case):</p>
  
  <pre><code>Cannot bind argument to parameter 'Path' because it is null.
      + CategoryInfo          : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
      + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand
      + PSComputerName        : localhost
  </code></pre>
  
  <p>Could someone please explain why result of passing <code>MyCopyReturn</code> is different from passing <code>MyCopy</code>?</p>
  
  <pre><code>function MyCopy ($from, $to) {
      Copy-Item $from $to -Force -Recurse -Verbose
  }
  
  function MyCopyReturn ($from, $to) {
      Copy-Item $from $to -Force -Recurse -Verbose
      return
  }
  
  $credential = Get-Credential
  $computerName = 'localhost'
  $session = New-PSSession -ComputerName $computerName -Credential $credential -Authentication Credssp
  # src is a file
  $src = "c:\sandbox\a\test"
  # dest is a directory
  $dest = "c:\sandbox\b"
  
  # 1.
  # MyCopy works fine without giving session
  Invoke-Command -ScriptBlock ${function:MyCopy} -ArgumentList $src,$dest
  
  # 2.
  # MyCopy DOESN'T WORK when session is given
  Invoke-Command -Session $session -ScriptBlock ${function:MyCopy} -ArgumentList $src,$dest
  
  # 4.
  # MyCopyReturn works fine without the session
  Invoke-Command -ScriptBlock ${function:MyCopyReturn} -ArgumentList $src,$dest
  
  # 3.
  # MyCopyReturn works fine with session too
  Invoke-Command -Session $session -ScriptBlock ${function:MyCopyReturn} -ArgumentList $src,$dest
  
  Remove-PSSession $session
  </code></pre>