1

I've had a problem recently with PowerShell reporting that a method does not exist on an object, while Get-Member reports that it does. This doesn't happen with all methods, but I have had it happen in more than one instance.

Here's a screenshot of one example in the ISE.

enter image description here

2
  • It seems like .Remove expects two int parameters and you're feeding it a string. Try $x.Remove(0,1) instead?
    – Nathan C
    Jan 6, 2014 at 20:56
  • @NathanC Shouldn't it be giving me a different error then, instead of "doesn't contain a method"? ETA: $x.Remove(0,1) fails with same error.
    – Iszi
    Jan 6, 2014 at 21:00

1 Answer 1

1

Your $x variable holds an array. When you pipe an array into a function, the pipeline doesn't pass the array instance into the function; it passes the array's elements into the function's process block, one at a time.

Get-Member is actually showing you the members of the string 'c' -- the last element in the array -- rather than the members of the array itself. (Notice that the output in your screenshot starts with TypeName: System.String.)

If you want to see the members for $x (the array) itself, pass it into Get-Member using the -InputObject parameter, instead of piping it in.

$X='a','b','c'; gm -InputObject $x

Then you'll see that your $x is of type System.Object[] and doesn't contain a Remove method. (You can't remove items because that would change the array's length, and arrays in .NET are fixed-length.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .