Script has two variables when done, but when I pipe to SELECT-object only first one returns data to console

What you’re experiencing is merely a display problem:

  • Both your Select-Object calls produce output objects with 4 or fewer properties whose types do not have explicit formatting data associated with them (as reported by Get-FormatData).

  • This causes PowerShell’s for-display output formatting system to implicitly render them via the Format-Table cmdlet.

  • The display columns that Format-Table uses are locked in based on the properties of the very first object that Format-Table receives.

  • Therefore, your second Select-Object call, whose output objects share no properties with the objects output by the first one, effectively produces no visible output – however, the objects are sent to the success output stream and are available for programmatic processing.

A simple demonstration:

& {
  # This locks in Month and Year as the display columns of the output table.
  Get-Date   | Select-Object Month, Year
  # This command's output will effectively be invisible,
  # because the property set Name, Attributes does not overlap with
  # Month, Year
  Get-Item \ | Select-Object Name, Attributes
}

The output will look something like this – note how the second statement’s output is effectively invisible (save for an extra blank line):

Month Year
----- ----
    9 2021


Note the problem can even affect a single statement that outputs objects of disparate types (whose types don’t have associated formatting data); e.g.:
(Get-Date | Select-Object Year), (Get-Item \ | Select-Object Name)


Workarounds:

  • Applying | Format-List to the command above makes all objects visible, though obviously changes the display format.

  • Intra-script you could pipe each Select-Object pipeline to Out-Host to force instant, pipeline-specific formatting, but – given that the results are sent directly to the host rather than to the success output stream – this technique precludes further programmatic processing.


Potential future improvements:

GitHub issue #7871 proposes at least issuing a warning if output objects effectively become invisible.

Leave a Comment