When I call the test.bat script from a cmd Window (or from a task), “after array” is never displayed, nor the $ArrayWithHeader array.
Strangely, if I add the command “$PSVersiontable.PSversion” as first line of my script everything is displayed as expected.
I tried adding some try{}catch{} code to it but it didn’t change anything. Calling the ps script directly works fine of course.
I am using Powershell version 5.1.19041.1320 and saw the same behavior on Windows 10 and Windows 2016 server.
Any idea why displaying $PSVersiontable.PSversion would cause the script behavior to change and why it doesn’t work without it?
thanks for your response even if it doesn’t tell me why my code is failing without the “$PSVerstionTable.PSVersion” command. I have no control over the .bat script as it is a tool I am using and my Powershell script is also more complicated, I only created the simplest example I could to reproduce the problem, as it seems that any array with headers will cause the problem. I believe the .bat script, which is also much longer, calls PS scripts that way to catch any error not handled correctly in the PS scripts.
My question was not about creating the array, your code works fine but is still not displayed when called from my bat script.
My problem is that when my PS script is called from a bat script containing
nothing is displayed, except if I use the command “$PSVersiontable.PSversion” first. It seems to be the case for any array with column headers.
I understand that it could have been more appropriate to report is as a bug to Microsoft but I would probably wait very long before it is answered, if at all.
thanks
I don’t know what to advice for you. I cannot explain. I just can suggest some fixes to your issue.
The output will show up when you call PowerShell in your CMD with the parameter -file instead of the call operator &.
The output will show up when you call PowerShell in your CMD without the parameter -NonInteractive.
The output will show up when you pipe your array inside your PowerShell script (or results) to Out-String.