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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With