Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum length of the -ArgumentList parameter of the Start-Process cmdlet

Tags:

powershell

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?

like image 345
Adwaenyth Avatar asked Sep 02 '25 09:09

Adwaenyth


1 Answers

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.

like image 149
TessellatingHeckler Avatar answered Sep 05 '25 01:09

TessellatingHeckler