(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
- What is the reason of this difference between C, E and F ?
- How do I detect that in advance (in future)?
- How do I fix that?
Format-TableorOut-Host.;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 whyFormat-Table/Out-Hostsolve it.