Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a Name NoteProperty to an object?

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
like image 752
jrara Avatar asked Oct 11 '11 08:10

jrara


2 Answers

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
like image 136
Roman Kuzmin Avatar answered Sep 28 '22 04:09

Roman Kuzmin


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
like image 41
jhamm Avatar answered Sep 28 '22 05:09

jhamm