# Displaying array with headers causes PS script to end when called from a cmd script

**URL:** <https://forums.powershell.org/t/displaying-array-with-headers-causes-ps-script-to-end-when-called-from-a-cmd-script/18461>\
**Category:** PowerShell Help\
**Created:** [January 29, 2022, 1:00am UTC](https://forums.powershell.org/t/displaying-array-with-headers-causes-ps-script-to-end-when-called-from-a-cmd-script/18461 "2022-01-29T01:00:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstoeckli](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@lstoeckli](https://forums.powershell.org/u/lstoeckli)\
**Post date:** [January 29, 2022, 1:00am UTC](https://forums.powershell.org/t/displaying-array-with-headers-causes-ps-script-to-end-when-called-from-a-cmd-script/18461/1 "2022-01-29T01:00:02Z")

</div>

Hi,  
I have a test.bat script with the following 2 lines:

```auto
powershell "&{&'C:\tmp\test.ps1';exit $LastExitCode}" -Noninteractive 
echo %errorlevel%

```

my C:\tmp\test.ps1 script contains the following lines:

```auto
[System.Collections.ArrayList]$ArrayWithHeader = @()
$ArrayOfValues = @('a@b.com','b@c.com','c@d.com','d@e.com')
foreach ($obj in $ArrayOfValues) 
{ 
     $val = [pscustomobject]@{'mail'=$obj;'date'=(Get-Date)}
     $ArrayWithHeader.add($val) | Out-Null
     $val=$null
}
$ArrayWithHeader
"after array"

```

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,

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 29, 2022, 1:15am UTC](https://forums.powershell.org/t/displaying-array-with-headers-causes-ps-script-to-end-when-called-from-a-cmd-script/18461/2 "2022-01-29T01:15:47Z")

</div>

l. stoeckli (… sounds swiss … 😉 )  
Welcome to the forum. 👋🏽

Please do us a favour and format the code you post in this forum as code using the _preformatted text_ button ( **\</\>** ).

> [@Welcome to the new PowerShell Forums](https://forums.powershell.org/t/welcome-to-the-new-powershell-forums/7/6):
>
> How to format code in [PowerShell.org](http://PowerShell.org) forums. [Codeformatting\_1]

Thanks in advance.

The command line to call PowerShell with a file looks a little weird to me. I’d write it this way:

```auto
PowerShell.exe -File 'C:\tmp\test.ps1' -Noninteractive

```

Your PowerShellCode looks a little convoluted as well. You may try it this way:

```auto
$ArrayOfValues = @('a@b.com', 'b@c.com', 'c@d.com', 'd@e.com')
$ArrayWithHeader =
foreach ($obj in $ArrayOfValues) {
    [pscustomobject]@{
        mail = $obj
        date = Get-Date
    }
}
$ArrayWithHeader

```

---

<div class="post-metadata">

**Author:** ![lstoeckli](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@lstoeckli](https://forums.powershell.org/u/lstoeckli)\
**Post date:** [January 29, 2022, 1:59am UTC](https://forums.powershell.org/t/displaying-array-with-headers-causes-ps-script-to-end-when-called-from-a-cmd-script/18461/3 "2022-01-29T01:59:16Z")

</div>

Hi Olaf,

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 29, 2022, 11:52am UTC](https://forums.powershell.org/t/displaying-array-with-headers-causes-ps-script-to-end-when-called-from-a-cmd-script/18461/4 "2022-01-29T11:52:27Z")

</div>

How about this?

```auto
$ArrayOfValues = @('a@b.com', 'b@c.com', 'c@d.com', 'd@e.com')

$ArrayOfValues |
    Select-Object -Property @{Name = 'mail'; Expression={$_}},
                            @{Name = 'date'; Expression={Get-Date}}

```

---

<div class="post-metadata">

**Author:** ![lstoeckli](https://avatars.discourse-cdn.com/v4/letter/l/e9c0ed/32.png) [@lstoeckli](https://forums.powershell.org/u/lstoeckli)\
**Post date:** [January 31, 2022, 5:32pm UTC](https://forums.powershell.org/t/displaying-array-with-headers-causes-ps-script-to-end-when-called-from-a-cmd-script/18461/5 "2022-01-31T17:32:52Z")

</div>

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

```auto
powershell "&{&'C:\tmp\test.ps1';exit $LastExitCode}" -Noninteractive 
echo %errorlevel%

```

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 31, 2022, 5:50pm UTC](https://forums.powershell.org/t/displaying-array-with-headers-causes-ps-script-to-end-when-called-from-a-cmd-script/18461/6 "2022-01-31T17:50:35Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:19pm UTC](https://forums.powershell.org/t/displaying-array-with-headers-causes-ps-script-to-end-when-called-from-a-cmd-script/18461/7 "2024-05-16T20:19:40Z")

</div>

Closed on May 16, 2024, 8:19pm UTC


