Powershell to avoid cmd 8191 character limit

Viewed 2660

I'm creating a PowerShell that runs a simple Java program using:

Start-Process -filepath .\java.exe -ArgumentList "Input" -NoNewWindow

The Input class in this case simply accepts and prints a string.

I'm expecting strings with length greater than the cmd.exe 8191 char limit, so I need to run it in PS.

Current scenario:

  • Running the above command in the PS directly works and Input accepts string greaters than 8191 char.
  • Saving the command in a ps1 file and run it, a PS windows appears but cmd runs behind the scene as it hits the 8191 character limit.

I tried adding -Wait as well, but not luck.

Any idea? Thank you

1 Answers

As you state, the max. length of a command line in cmd.exe (at the interactive Command Prompt and in batch files) is officially 8,191 characters - see the documentation. If you try to submit a command line via the Run dialog (Win-R), the limit appears to be even lower.

However, this does not apply to command lines in PowerShell, where the limit is much higher - 32,764 characters.

In other words: commands launched from within PowerShell aren't subject to the 8,191-character limit - irrespective of whether you use Start-Process or Invoke-Expression (neither of which you should use for invoking console applications / scripts - see this answer and this answer) or by direct invocation (by file path, possibly preceded by &, the call operator - e.g., .\javaRun.ps1 or & .\javaRun.ps1) - none of these methods involve cmd.exe

As for calling external programs from PowerShell:

  • At least .NET-based executables are capable of receiving command lines up to PowerShell's 32,764-character limit.

  • I don't know about executables using different runtimes, such as java.exe; but if they turn out to be the bottleneck, you won't be able to overcome this problem from PowerShell, and will instead have to find a different way to pass input to the target program, such as via standard input or an aux. file.

Related