Reputation: 998
I am trying to remove what I thought was an element of an array that is a member of a PSObject.
$obj = New-Object psobject
$obj | Add-Member -MemberType NoteProperty -Name member -Value @()
$obj.member += $null
$obj.member[0] = "A"
$obj.member += $null
$obj.member[1] = "B"
so far so good. $obj.member behaves like an array in that i can call individual element and convert to string using -join. However, when I try to remove an element, I find out that $obj.member is not an array at all. $obj.member|gm tells me it's a String and when I call $obj.member.removeRange(), I get an error: stating: [System.Object[]] doesn't contain a method named 'removeRange'.
So.. it looks like a string, behaves like an array, and is actually an Object.
I was under impression that if i define the value of the NoteProperty type member of an object as @(), that member will be an array--evidently this is not true, but never occurred to me as i was able to -join the elements.
Please explain what is going on here.
Ultimately, I am trying to have an Object member be an array, so I can add AND remove elements.
Thanks!!
Upvotes: 0
Views: 4057
Reputation: 16792
You are properly adding the member, and it is indeed an array.
$obj.member | gm tells me it's a String
Piping to Get-Member
unrolls any collections and displays information about the objects inside the collection, not the collection itself. For example, 1,2,3 | gm
tells me about members on the int
type. If I want to know about the collection itself, you can do it like this:
,(1,2,3) | gm
,$obj.Member | gm
when I call $obj.member.removeRange(), I get an error
That's because the array type doesn't have a RemoveRange
method, as you will see when you pipe to gm
. Also see docs on System.Array
here.
Upvotes: 2