I have a powershell script that is called by chef that works on windows 2012r2, but fails on windows 2016 (powershell 5.1.14393.1884) .
I paste into the powershell window the following 2 commands
$letters = New-Object System.Collections.ArrayList
$letters.AddRange( ('F','G') );
And I get this error
You cannot call a method on a null-valued expression.
Looking at the output, it seems that the order of the commands is reversed. AddRange is before New-Object.
Things I've tried
Add ; to the end of each command
$letters = New-Object System.Collections.ArrayList;
$letters.AddRange( ('F','G') );
Call ArrayList with ($null) in case the constructor requires it
$letters = New-Object System.Collections.ArrayList($null);
and
$letters = New-Object System.Collections.ArrayList(,$null);
Cast AddRange to [void] as recommended here
$letters = New-Object System.Collections.ArrayList;
[void] $letters.AddRange( ('F','G') );
Why does powershell 5.1 run these commands out of order? Is this a bug?
Update
To clarify, the commands run out of order when I type/paste them into a powershell window. I get the exact same error when chef runs the commands. Since chef is 'shelling-out' it is effectively doing typing for me.
Update
There are actually 2 issues going on. andyb identified the known bug where ctrl +v behaves differently than right clicking to paste in windows 10/windows server 2016

ArrayList? PowerShell has excellent native array support. You could just write that as$letters = @('F', 'G')and get an array with those letters in it. Beyond that, can you include the script in its entirety or at least a MCVE that exhibits the behavior you're experiencing?