0

(TL;DR: I placed my questions below, at the bottom of my text)

A. I list optical drives this way:

PS C:\> Get-CimInstance Win32_LogicalDisk -Filter 'DriveType = 5'

DeviceID DriveType ProviderName VolumeName Size FreeSpace
-------- --------- ------------ ---------- ---- ---------
D:       5

B. I inspect my ISO file that way:

PS C:\> Get-DiskImage -ImagePath "C:\w10-test.iso"


Attached          : False
BlockSize         : 0
DevicePath        :
FileSize          : 4439830528
ImagePath         : C:\w10-test.iso
LogicalSectorSize : 2048
Number            :
Size              : 4439830528
StorageType       : 1
PSComputerName    :

C. If I combine cmdlets A and B on the same cmd-line (or run them in my script consecutively), I get concatenated output, as expected:

PS C:\> Get-CimInstance Win32_LogicalDisk -Filter 'DriveType = 5';
>> Get-DiskImage -ImagePath 'C:\w10-test.iso'

DeviceID DriveType ProviderName VolumeName Size FreeSpace
-------- --------- ------------ ---------- ---- ---------
D:       5

Attached          : False
BlockSize         : 0
DevicePath        :
FileSize          : 4439830528
ImagePath         : C:\w10-test.iso
LogicalSectorSize : 2048
Number            :
Size              : 4439830528
StorageType       : 1
PSComputerName    :

D. I want more details about the present volumes, so add Select-Object to A:

PS C:\> Get-CimInstance Win32_LogicalDisk -Filter 'DriveType = 5' | Select-Object DeviceID, Size, VolumeName, Description

DeviceID Size VolumeName Description
-------- ---- ---------- -----------
D:                       CD-ROM Disc

E. Now if I combine D and B, the output somehow gets messed up:

PS C:\> Get-CimInstance Win32_LogicalDisk -Filter 'DriveType = 5' | Select-Object DeviceID, Size, VolumeName, Description;
>> Get-DiskImage -ImagePath 'C:\w10-test.iso'

DeviceID Size       VolumeName Description
-------- ----       ---------- -----------
D:                             CD-ROM Disc
         4439830528

F. Now if I swap D + B into B + D, the output gets OK again:

PS C:\> Get-DiskImage -ImagePath 'C:\w10-test.iso';
>> Get-CimInstance Win32_LogicalDisk -Filter 'DriveType = 5' | Select-Object DeviceID, Size, VolumeName, Description


Attached          : False
BlockSize         : 0
DevicePath        :
FileSize          : 4439830528
ImagePath         : C:\w10-test.iso
LogicalSectorSize : 2048
Number            :
Size              : 4439830528
StorageType       : 1
PSComputerName    :

DeviceID    : D:
Size        :
VolumeName  :
Description : CD-ROM Disc

  1. What is the reason of this difference between C, E and F ?
  2. How do I detect that in advance (in future)?
  3. How do I fix that?
5
  • 1
    Has been discussed many times in the past, you simply don't output objects with different properties in the same pipeline and if you do you should use Format-Table or Out-Host. Commented Mar 24 at 21:34
  • @SantiagoSquarzon, thanks for the tip. As a PSh newbie, (1) how do I know these two lines are run in the same pipeline? On *nix systems every line is ran in its' own pipeline. (2) Do you know why does mixing work if the F case? Commented Mar 24 at 21:44
  • 1
    1. In PowerShell every statement you run even if separated with ; happens in a single pipeline. Also all code in a script is executed in the same pipeline. 2. The reason is the objects are shown as a list instead of as a table. The issue you have faced is a table formatting issue, the table formatting is unable to determine what do with objects that have different properties in a single pipeline so you get this unexpected behavior. The 3 linked answer explain in depth this issue and why Format-Table / Out-Host solve it. Commented Mar 24 at 21:59
  • 1
    Perhaps an easier way to see it reg. point 1., everything that happens in one REPL, happens in a single pipeline managed by PowerShell’s runspace Commented Mar 24 at 22:12
  • @SantiagoSquarzon, perhaps! It's good to see your addendum (because the same happens when the 2nd statements is split off into the next line too). Though your last answer targets something else, not what I asked there as question (2). Ie. instead of explaining why table formatting works just fine, you told about when it doesn't -- a different aspect. Also you did't explain how to know of such unexpected behavior in advance, before testing the statements. Thanks for the 3rd link tip! If it won't explain the issue to me thoroughly enough, I will be back when I done with it. :) Cheers! Commented Mar 25 at 15:44

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.