Reputation: 6424
Why is the output of following code blocks different? The object that ConvertTo-Html cmdlet takes as input is not the same in both cases? The output of code block 1 is what I was expecting.
Code 1:
$psdrive = Get-PSDrive
$psdrive | ConvertTo-Html
Code 2:
$psdrive = Get-PSDrive
ConvertTo-Html -InputObject $psdrive
Here the outputs.
Output 1:
...
<tr>
<th>Used</th>
<th>Free</th>
<th>CurrentLocation</th>
<th>Name</th>
<th>Provider</th>
<th>Root</th>
<th>Description</th>
<th>MaximumSize</th>
<th>Credential</th>
<th>DisplayRoot</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>Alias</td>
<td>Microsoft.PowerShell.Core\Alias</td>
<td></td>
<td>Drive that contains a view of the aliases stored in a session state</td>
<td></td>
<td>System.Management.Automation.PSCredential</td>
<td></td>
</tr>
<tr>
<td>72876965888</td>
<td>12914372608</td>
<td>Windows\system32</td>
<td>C</td>
<td>Microsoft.PowerShell.Core\FileSystem</td>
<td>C:\</td>
<td></td>
<td></td>
<td>System.Management.Automation.PSCredential</td>
<td></td>
</tr>
...
Output 2:
...
<tr>
<th>Count</th>
<th>Length</th>
<th>LongLength</th>
<th>Rank</th>
<th>SyncRoot</th>
<th>IsReadOnly</th>
<th>IsFixedSize</th>
<th>IsSynchronized</th>
</tr>
<tr>
<td>12</td>
<td>12</td>
<td>12</td>
<td>1</td>
<td>System.Object[]</td>
<td>False</td>
<td>True</td>
<td>False</td>
</tr>
</table>
...
Upvotes: 1
Views: 310
Reputation: 17094
That is how the pipeline behaves.
Get-PsDrive
will return an array (if there is more than one drive).
$psdrive.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
By piping $psdive
(which should be called $psdrives
, plural), you pass every single item of the array to ConvertTo-Html
, so they will be formatted properly.
By passing the actual array as -InputObject
it will convert the array itself into HTML, that's why you see the array members such as Count
, Length
etc. You would get the same result if you passed an empty array: ConvertTo-Html -InputObject @()
You can easily see that behavior, check this out:
# will return the members of the drives (Root, MaximumSize, ...)
$psdrive | Get-Member
# will return the members of the array that contains the drives (Add, Count, ...)
Get-Member -InputObject $psdrive
Conclusion: The first version, using the pipeline, is the correct way to do it.
Upvotes: 1