4

I'd like to use Start-Process to call a programm from PS and pass a bunch of arguments to that call that said program should process in background.

It might occasionally happen, that the total list of supplied arguments to that program might be hundreds (something like 200-300 in total), each again a string of up to 32 bytes length. I've tried to find out about the maximum length of -ArgumentList but couldn't find any reference so far.

I doubt I would run into any problems with the amount of arguments I would supply, but it did bug me, how many Arguments or how long in total might the -ArgumentList parameter actually be?

Adwaenyth
  • 2,020
  • 12
  • 24
  • Argument list is an array of objects.There shouldn't be any limit per se. If you have problems, they won't come from the *number* of arguments. – David Brabant Nov 10 '16 at 07:37
  • @DavidBrabant My guess would be, that `Start-Process` combines the arguments to a string formatted with spaces and quotation marks, so 200-300 * (32 + 1) would be something like 6000-9000 characters. Thus it would be the maximum length any windows command line could have, but I haven't found any resource so far so I thought it might be worth a stack question. – Adwaenyth Nov 10 '16 at 07:41
  • 1
    https://blogs.msdn.microsoft.com/oldnewthing/20031210-00/?p=41553 and https://blogs.msdn.microsoft.com/oldnewthing/20031211-00/?p=41543 – David Brabant Nov 10 '16 at 07:44
  • 1
    @DavidBrabant Yeah after testing around a little with TessellatingHeckler's answer, I would come to the same conclusion. The maximum length is a total input string of 32,767 including the program executable name - at least on a Win10 machine. – Adwaenyth Nov 10 '16 at 07:54

2 Answers2

7

Combined length of 8191 characters, maybe. Or maybe it depends on the program you're running.

Source: Trial and error (Windows 8.1 / PSv4):

Start-Process -FilePath cmd -ArgumentList (@('/k','echo 1') + (2..1852))
# works

Start-Process -FilePath cmd -ArgumentList (@('/k','echo 1') + (2..1853))
# doesn't work

Around 6769 it triggers an exception:

PS C:\> Start-Process -FilePath cmd -ArgumentList (@('/k','echo 1') + (2..6768))
Start-Process : This command cannot be run due to the error: The filename or extension is too long.
At line:1 char:1
+ Start-Process -FilePath cmd -ArgumentList (@('/k','echo 1') + (2..676 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

But if I shift the numbers a bit (2..1852|%{$_*100}) then it fails sooner. Suggesting it's not the number of arguments which matters, but the string length of the combined result.

((@('/k','echo 1') + (2..1852)) -join " ").Length
# 8160 when it works, 8165 when it breaks

Google for 8165 limit cmd and get:

Maximum Length of Command Line String

https://support.microsoft.com/en-gb/kb/830473

On computers running Microsoft Windows XP or later, the maximum length of the string that you can use at the command prompt is 8191 characters.

So, either 8191 characters or ... maybe it depends on the program you're calling.

300 * 32 would break that.

But then again, if you've already got a program which can handle it - start-process appears to have no problem with an array of 1,800 items as an argument list.

Community
  • 1
  • 1
TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
  • 1
    Tested it on Windows 10 pro 1607 it seems to be a limit around 32,767 (exception was thrown slightly below at 32,742, haven't figured out why so far) characters total length - considering that the argument list would indeed be joined to a space separated string. So it also seems OS dependent. So you might want to update your answer. – Adwaenyth Nov 10 '16 at 07:50
3

I have found that the length limit is significantly less when using the -Credential parameter as part of the Start-Process command. I get a "parameter is incorrect" if I exceed about 900 characters. Less than that and it works fine.

Without using -Credential, I have been able to submit an argument that is much, much longer (6000+ characters).

Azsgy
  • 3,139
  • 2
  • 29
  • 40
Dean
  • 31
  • 1