title: Max size of ScriptBlock / InitializationScript for Start-Job in PowerShell
body: <p>When you start a new job with <code>Start-Job</code>, you can pass it a <code>ScriptBlock</code> and a <code>InitializationScript</code> for example:</p>
  
  <pre><code>Function FOO {
    Write-Host "HEY"
  } 
  Start-Job -ScriptBlock {FOO} -InitializationScript {
    Function Foo { $function:FOO }
  } | Wait-Job | Receive-Job
  </code></pre>
  
  <p>There seems to be a limit to the size of the initialization script you can pass, if it is too big then you get an error such as</p>
  
  <pre>[localhost] An error occurred while starting the background process. Error
  reported: The filename or extension is too long.
      + CategoryInfo          : OpenError: (localhost:String) [], PSRemotingTransportException
      + FullyQualifiedErrorId : -2147467259,PSSessionStateBroken</pre>
  
  <p>Behind the scenes, PowerShell is creating a new process and passing <code>InitializationScript</code> as a Base64 encoded command line parameter.</p>
  
  <p>According to the Win32 <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx" rel="nofollow"><code>CreateProcess()</code></a> function, the max size of the command ine is 32,768 characters. So obviously if your Base64 encoded <code>InitializationScript</code> is getting near this size then you will probably get an error.</p>
  
  <p>I haven't yet found a limit for the size of the <code>ScriptBlock</code> parameter. Can someone confirm that there is no limit?</p>
  
  <p>I assume that there is no limit because it looks like the <code>ScriptBlock</code> is transmitted to the child process via standard input?</p>