Advanced functions | Parameter binding by PSTypeName

Hi guys,

I recently discovered the possibility to bind a parameter by a custom type name. The common approach seems to be using [PSCustomObject] as parameter type if you don’t use classes.

Just for clarify a little example:

My questions here are:

  • Does this scenario of using PSTypeName for parameter binding respect the best practice and common used style guide?
  • It seems to fill the gap for using the PSCustomObjects and still be able to bind it properly. So why is this so rare to find?
  • Is this a newer feature? Was this introduced in v4 or v5?
Yes, this is a repost from PowerShellPracticeAndStyle #107. - But my purpose is to expand scope into this awesome community :)

I have never seen this. Ever. Is this even documented anywhere? I deliberately pored over the various parameter attributes for a few hours to make sure I know at least most of them, but this never cropped up anywhere.

I think that in most cases, it may be a better idea to use a class. However, there are probably cases that make that very difficult or impossible (classes in PS are unfortunately still very limited). In those cases, this would be a boon.

The primary issue with this is that a type name means essentially nothing. It can affect the object’s display modes and default display set if you’ve done that with it, but that’s about it.

This isn’t really validating anything, honestly. It’s not hard to tack on a type name to any custom object, so this doesn’t do what input validation is meant to do – avoid having to deal with the messiness that comes with manually validating. You’ll still have to verify that the object’s you’re given have the right properties and so forth.

So… In a pinch, this is quite nifty. But when you have the choice, use a proper class.

I was looking for an easy way to bind a PSCustomObject with a custom TypeName in advanced function. So I recently discovered this solution and it does exactly my needs. I know there is no validation for any members but it avoids users to use any other PSCustomObject as parameter. So I wondered why this is used so rarely. And no - I didn’t found any documentation therefore.

My next question was if this is available since version 4. And yes it works ^^

Every time I’m forced to support v4 I rally thinking about switching to this solution :smiley:

Thanks for your feedback!