Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Array and ConvertTo-HTML [closed]

I cant for the life of me get the contents of a string array (well its not strictly typed but all the items in the array are strings) to become a table, i am trying to use ConvertTo-HTML.

For example:

$arr = @("One", "Two")
$arr | ConvertTo-HTML

I have tried -As List, using fragments, even using InputObject, is it even possible ?

Edit: i simply want to know if there is anyway to use an array as the input for the ConvertTo-HTML cmdlet, all it does is make a table with the length property of the strings.


1 Answers

ConvertTo-HTML will take a collection of .NET object and output the HTML for a nice table diplaying the objects' public properties.

System.String only has 1 public property: Length. So you will get a table with 1 column: Length. This is by design and consistent with normal behavior.

But it sounds like you don't want a table of the public properties of your collection objects. You want a table with the Length property, and a column with the ToString() value.

To achieve this, we can simply create a custom object whose properties match this goal:

'one','two','three' |%{ New-Object PsObject -Prop @{Length = $_.Length; String = $_} } | ConvertTo-HTML
like image 190
latkin Avatar answered Feb 01 '26 05:02

latkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!