I've encountered some Powershell behaviour that I didn't expect while using ForEach-Object
and ToString
. Digits are being replaced automatically and I can't quite graps the rule for the substitution from the output alone.
Here's a small simplified example:
PS C:\Users\Telefonmann> 1..3 | ForEach-Object {$_.ToString('test_0_1')}
test_1_1
test_2_1
test_3_1
PS C:\Users\Telefonmann> 1..3 | ForEach-Object {$_.ToString('test_0_0')}
test_0_1
test_0_2
test_0_3
PS C:\Users\Telefonmann> 1..3 | ForEach-Object {$_.ToString("test_0$_\_0")}
test_01_1
test_02_2
test_03_3
PS C:\Users\Telefonmann> 1..3 | ForEach-Object {$_.ToString("test_0$_\_$_")}
test_11_1
test_22_2
test_33_3
In the first example the 0 is replaced, in the second only the last 0, in the third the placeholder and the 0 are replaced and in the last example the 0 and, of course, the placeholders are replaced. Does Powershell just see a string with a 0 and then assumes that the last 0 in any string is supposed to be a counter?
What's the term for this behaviour / is there some documentation for it? How do I disable it?