title: No output while running script or function. Only output on command line
body: <p>I have a script file that contains the code below.<br>
  It runs the same code 3 times.  </p>
  
  <ol>
  <li>In the script, inside a function</li>
  <li>In the script, outside a function</li>
  <li>Out of the script, on the command line</li>
  </ol>
  
  <p>Source code:</p>
  
  <pre><code>Clear-Host
  
  $myKeys = @('key1','key2','key3','key4','key5')
  $myValues = @('one','two','three','four','five')
  
  # Declare function
  Function Invoke-MyArray
  {
      [CmdletBinding()]   param ()
  
      Write-Output "Function: $($MyInvocation.Mycommand)"
  
      $myLocalArray = @()
  
      foreach ($element in $myKeys)
      {
          $key = $element
          $val = $myValues[$myKeys.IndexOf($key)]
  
          $myObject = New-Object -TypeName PSObject -Property @{
              key = $key
              val = $val
          }   
  
          $myLocalArray += $myObject
      }
  
      Write-Output '### 1 ### $myArray'
      Write-Output '------------------'
      $myArray
  
      Write-Output '### 2 ### $myArray.getType()'
      Write-Output '----------------------------'
      $myArray.getType()
  
      Write-Output '### 3 ### $myArray.getType() | Out-String'
      Write-Output '-----------------------------------------'
      $myArray.getType() | Out-String
  
  
  
      Write-Output '### 4 ### $myArray[0]'
      Write-Output '---------------------'
      $myArray[0]
  
      Write-Output '### 5 ### $myArray[0].getType()'
      Write-Output '-------------------------------'
      $myArray[0].getType()
  
      Write-Output '### 6 ### $myArray[0].getType() | Out-String'
      Write-Output '--------------------------------------------'
      $myArray[0].getType() | Out-String
  
  }
  
  # Call function
  Write-Output '====================='
  Write-Output '=== Call function ==='
  Write-Output '====================='
  Invoke-MyArray
  
  Write-Output '==============================================='
  Write-Output '=== Execute same code, without the function ==='
  Write-Output '==============================================='
  # Execute same code, without the function
  $myArray = @()
  
  foreach ($element in $myKeys)
  {
      $key = $element
      $val = $myValues[$myKeys.IndexOf($key)]
  
      $myObject = New-Object -TypeName PSObject -Property @{
          key = $key
          val = $val
      }   
  
      $myArray += $myObject
  }
  
  
  Write-Output '### 1 ### $myArray'
  Write-Output '------------------'
  $myArray
  
  Write-Output '### 2 ### $myArray.getType()'
  Write-Output '----------------------------'
  $myArray.getType()
  
  Write-Output '### 3 ### $myArray.getType() | Out-String'
  Write-Output '-----------------------------------------'
  $myArray.getType() | Out-String
  
  
  
  Write-Output '### 4 ### $myArray[0]'
  Write-Output '---------------------'
  $myArray[0]
  
  Write-Output '### 5 ### $myArray[0].getType()'
  Write-Output '-------------------------------'
  $myArray[0].getType()
  
  Write-Output '### 6 ### $myArray[0].getType() | Out-String'
  Write-Output '--------------------------------------------'
  $myArray[0].getType() | Out-String
  
  
  Write-Output '===================================='
  Write-Output '=== Executed on the command line ==='
  Write-Output '===================================='
  </code></pre>
  
  <p>Resulting output:</p>
  
  <pre><code>=====================
  === Call function ===
  =====================
  Function: Invoke-MyArray
  ### 1 ### $myArray
  ------------------
  
  key  val  
  ---  ---  
  key1 one  
  key2 two  
  key3 three
  key4 four 
  key5 five 
  ### 2 ### $myArray.getType()
  ----------------------------
  
  ### 3 ### $myArray.getType() | Out-String
  -----------------------------------------
  
  IsPublic IsSerial Name                                     BaseType                                                                               
  -------- -------- ----                                     --------                                                                               
  True     True     Object[]                                 System.Array                                                                           
  
  
  
  ### 4 ### $myArray[0]
  ---------------------
  key1 one  
  ### 5 ### $myArray[0].getType()
  -------------------------------
  
  ### 6 ### $myArray[0].getType() | Out-String
  --------------------------------------------
  
  IsPublic IsSerial Name                                     BaseType                                                                               
  -------- -------- ----                                     --------                                                                               
  True     False    PSCustomObject                           System.Object                                                                          
  
  
  
  ===============================================
  === Execute same code, without the function ===
  ===============================================
  ### 1 ### $myArray
  ------------------
  key1 one  
  key2 two  
  key3 three
  key4 four 
  key5 five 
  ### 2 ### $myArray.getType()
  ----------------------------
  
  ### 3 ### $myArray.getType() | Out-String
  -----------------------------------------
  
  IsPublic IsSerial Name                                     BaseType                                                                               
  -------- -------- ----                                     --------                                                                               
  True     True     Object[]                                 System.Array                                                                           
  
  
  
  ### 4 ### $myArray[0]
  ---------------------
  key1 one  
  ### 5 ### $myArray[0].getType()
  -------------------------------
  
  ### 6 ### $myArray[0].getType() | Out-String
  --------------------------------------------
  
  IsPublic IsSerial Name                                     BaseType                                                                               
  -------- -------- ----                                     --------                                                                               
  True     False    PSCustomObject                           System.Object                                                                          
  
  
  
  ====================================
  === Executed on the command line ===
  ====================================
  
  
  PS C:\&gt;     Write-Output '### 1 ### $myArray'
      Write-Output '------------------'
  
  ### 1 ### $myArray
  ------------------
  PS C:\&gt; $myArray
  
  key  val  
  ---  ---  
  key1 one  
  key2 two  
  key3 three
  key4 four 
  key5 five 
  
  
  PS C:\&gt;     Write-Output '### 2 ### $myArray.getType()'
      Write-Output '----------------------------'
  
  ### 2 ### $myArray.getType()
  ----------------------------
  PS C:\&gt; $myArray.getType()
  
  IsPublic IsSerial Name                                     BaseType                                                                               
  -------- -------- ----                                     --------                                                                               
  True     True     Object[]                                 System.Array                                                                           
  
  
  PS C:\&gt;     Write-Output '### 3 ### $myArray.getType() | Out-String'
      Write-Output '-----------------------------------------'
  
  ### 3 ### $myArray.getType() | Out-String
  -----------------------------------------
  PS C:\&gt; $myArray.getType() | Out-String
  
  IsPublic IsSerial Name                                     BaseType                                                                               
  -------- -------- ----                                     --------                                                                               
  True     True     Object[]                                 System.Array                                                                           
  
  
  
  PS C:\&gt;     Write-Output '### 4 ### $myArray[0]'
      Write-Output '---------------------'
  
  ### 4 ### $myArray[0]
  ---------------------
  PS C:\&gt;     $myArray[0]
  
  
  key  val
  ---  ---
  key1 one
  
  
  PS C:\&gt;     Write-Output '### 5 ### $myArray[0].getType()'
      Write-Output '-------------------------------'
  
  ### 5 ### $myArray[0].getType()
  -------------------------------
  PS C:\&gt;     $myArray[0].getType()
  
  
  IsPublic IsSerial Name                                     BaseType                                                                               
  -------- -------- ----                                     --------                                                                               
  True     False    PSCustomObject                           System.Object                                                                          
  
  
  PS C:\&gt;     Write-Output '### 6 ### $myArray[0].getType() | Out-String'
      Write-Output '--------------------------------------------'
  
  ### 6 ### $myArray[0].getType() | Out-String
  --------------------------------------------
  PS C:\&gt;     $myArray[0].getType() | Out-String
  
  
  IsPublic IsSerial Name                                     BaseType                                                                               
  -------- -------- ----                                     --------                                                                               
  True     False    PSCustomObject                           System.Object                                                                          
  
  
  PS C:\&gt; 
  </code></pre>
  
  <p>Notice that the <code>.getType()</code> call is always empty (no result), except when called directly from the command line.  </p>
  
  <p>Only when <code>.getType()</code> is piped to <code>Out-String</code>, we see a result being printed inside of the script.  </p>
  
  <p>Why can't I get an output inside my script and/or function with <code>.getType()</code> only?</p>
  
  <p><strong>EDIT</strong> (after being marked as duplicate by Ansgar Wiechers):<br>
  The <a href="https://stackoverflow.com/questions/31441054/formatting-issues-missing-data-when-multiple-commands-use-select-object-within">duplicate</a> contains a <a href="https://blogs.msdn.microsoft.com/powershell/2006/04/29/how-powershell-formatting-and-outputting-really-works/" rel="nofollow noreferrer">link</a> to an MSDN article that states:  </p>
  
  <blockquote>
    <p><em>Now, if there is not a registered view for a datatype, then
    Out-Default looks at the FIRST OBJECT IN THE STREAM to determine how
    many properties the object has 5 or more properties, it send the
    ENTIRE STREAM to Format-List, otherwise it sends the ENTIRE STREAM to
    Format-Table.  When it sends the stream to Format-Table, that command
    needs to generate columns.  <strong>It does this by looking at the properties
    of the FIRST OBJECT – those become the columns.  If the first Object
    has 2 properties, you’ll get a 2 column table even if all the other
    objects in the stream have 10 properties.</em></strong></p>
  </blockquote>
  
  <p><code>$myArray</code> is an array with objects that have 2 properties.<br>
  So, I would expect to get a 2-column table in the output stream.<br>
  Instead I get no output at all.<br>
  In that sense, it's not really a duplicate question, but it sure is related.</p>