I also suspect it is a memory problem, and PowerShell tends to come sometimes with strange errors when this occurs.
The only valid solutions I can see, is to or run the script in 2 parts (for example first the 1st half and the 2nd half of the switches.
Or try to run the script on a box with more memory. Eventually on a 64-bit OS