I understand that custom objects are created like this :
$obj=new-object psobject
and then I understand that you can add members (and values) like this :
$obj | Add-Member noteproperty name Jeff
Now the question is, how do you populate the object, add and remove “rows” of values ?
The only way around I found is to create an array and then push objects inside it, like this :
$array = @()
$array+=new-object PSObject -Property @{Name="Jeff"}
$array+=new-object PSObject -Property @{Name="Joe"}
$array+=new-object PSObject -Property @{Name="John"}
etc..
Is there a straight forward way to “increment” the values of the members in an object ?
$obj+=(Name=John)
doesn’t work.
Thanks
In your example above, I believe you end up with a System.Management.Automation.PSCustomObject, not an array. I use something similar to what you’re doing when I build reports that contain custom objects. If you’re really just using that to store one property, though, it’s probably overkill. You could just do something like this:
In the case that you really want to add new note properties to an object throughout a script, this is how I do it. Keep in mind PowerShell doesn’t like to add note properties with the same name, so if you do that you’ll have to simply set the property with =
Here’s an example of what I do: