How can add Name NoteProperty for an object? I tried:
$a = "This", "Is", "a", "cat"
$a | Add-Member -type NoteProperty -name Name
$a
but this doesn't seem to work.
The expected output is:
Name
----
This
Is
a
cat
This is the answer to the amended question:
$a = "This", "Is", "a", "cat"
$a | Select-Object @{Name='Name'; Expression={$_}}
Output, as requested, is
Name
----
This
Is
a
cat
Here is an example of how to take your example each value in $a, convert it to a PSObject with a Name and Value properties as well as using the Add-Member cmdlet. The ` is for line continuation. Because the Add-Member is being called in a pipeline, the -passThru property was used to pass the object with the new member on.
$a | %{ new-object psobject -property @{Name="String"; Value=$_}} `
| %{ Add-Member -inputObject $_ -passThru -type NoteProperty -name Note -Value Value}
I piped the output to | ft -auto to shrink the columns to fit here nicely.
Value Name Note
----- ---- ----
This String Value
Is String Value
a String Value
cat String Value
Another way of answering the updated question:
$a | %{new-object psobject -p @{Name=$_}
Expected output matches:
Name
----
This
Is
a
cat
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With