# Custom Types and pstypenames behavior

**URL:** <https://forums.powershell.org/t/custom-types-and-pstypenames-behavior/12222>\
**Category:** PowerShell Help\
**Created:** [March 20, 2019, 1:43am UTC](https://forums.powershell.org/t/custom-types-and-pstypenames-behavior/12222 "2019-03-20T01:43:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![atkinsroy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/atkinsroy/32/26_2.png) [@atkinsroy](https://forums.powershell.org/u/atkinsroy)\
**Post date:** [March 20, 2019, 1:43am UTC](https://forums.powershell.org/t/custom-types-and-pstypenames-behavior/12222/1 "2019-03-20T01:43:00Z")

</div>

I have something here that’s been driving me nuts. Here’s an example function:

[pre]  
function Get-Wibble {  
if (-not (Get-TypeData -TypeName ‘Wibble’)) {  
Update-TypeData -TypeName ‘Wibble’ -DefaultDisplayPropertySet Colour, Date  
}  
$Name = @(‘Red’,‘Yellow’,‘Puce’,‘Red’,‘Yellow’,‘Puce’)  
$Date = @(‘16/03/19’,‘16/03/19’,‘16/03/19’,‘17/03/19’,‘17/03/19’,‘16/03/19’)  
0…5 | ForEach-Object {  
[pscustomobject] @{  
PSTypeName=‘Wibble’  
Colour = $Name[$_]  
Date = $Date[$_]  
Message = “Something random $\_”  
}  
}  
}  
[/pre]

This works as expected with Get-Member, the typename is “Wibble”. However, If I type:

[pre]  
(get-wibble).pstypenames  
[/pre]

I expected to see the custom type at the top of the list, but I just get the types you’d normally see for a custom object. What is going on here?  
&nbsp;

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [March 20, 2019, 4:06pm UTC](https://forums.powershell.org/t/custom-types-and-pstypenames-behavior/12222/2 "2019-03-20T16:06:21Z")

</div>

Are you sure you are going about this in a prudent way?

See these for possible edification:

> **[PowerShell: Custom Objects with Format Files](https://jrich523.wordpress.com/2012/01/20/powershell-custom-objects-with-format-files/)**
>
> I was recently working on something and realized that the default output (everything) was getting rather annoying. I then realized that the object I had wasnt going to cut it and I had to create my…

> **[Custom objects and PSTypeName - PowerShell Station](https://powershellstation.com/2016/05/22/custom-objects-and-pstypename/)**
>
> A couple of weeks ago, Adam Bertram wrote a post which got me really excited. As an aside, if you’re not following Adam you’re missing out. He posts ridiculously often and writes high quality posts about all kinds of cool stuff. I’m writing about one...

> **[Working With Custom Types of Custom Objects In PowerShell](https://learn-powershell.net/2012/03/02/working-with-custom-types-of-custom-objects-in-powershell/)**
>
> Building custom object with PowerShell is a great way to quickly put together output for use with reporting or pipelining into another cmdlet.  It is also very simple to put together a quick o…

[https://learningintheopen.org/2011/12/22/microsoft-powershell-list-all-the-properties-for-an-object](https://learningintheopen.org/2011/12/22/microsoft-powershell-list-all-the-properties-for-an-object)

> **[PowerShell QuickTip: Using PSTypeNames to See the TypeName and Inheritance Chain](https://rkeithhill.wordpress.com/2007/10/28/powershell-quicktip-using-pstypenames-to-see-the-typename-and-inheritance-chain/)**
>
> I’ve mentioned plenty of times that PowerShell is all about .NET objects, well actually PowerShell "adapted" .NET objects.  Quite often it is handy to know which type of object you …

> **[PowerShell Quick Tip: Getting all of the members of an object](https://poshoholic.com/2010/06/21/powershell-quick-tip-getting-all-of-the-members-of-an-object/)**
>
> If you weren’t already aware, using the Get-Member cmdlet is essential to understanding what you can do with PowerShell.  Get-Member allows you to identify the members (properties, methods, ev…

---

<div class="post-metadata">

**Author:** ![atkinsroy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/atkinsroy/32/26_2.png) [@atkinsroy](https://forums.powershell.org/u/atkinsroy)\
**Post date:** [March 21, 2019, 5:02am UTC](https://forums.powershell.org/t/custom-types-and-pstypenames-behavior/12222/3 "2019-03-21T05:02:42Z")

</div>

Thanks. I’m not sure I understand the comment about prudent. You link to articles I’ve read that are using the pstypenames method.

Let me try again with two examples. I’m asking why, in terms of enumerating the type names of the custom object, does this not work:

[pre]  
$Name = @(‘Red’,‘Yellow’,‘Puce’,‘Red’,‘Yellow’,‘Puce’)  
$Date = @(‘16/03/19’,‘16/03/19’,‘16/03/19’,‘17/03/19’,‘17/03/19’,‘16/03/19’)  
$test = 0…5 | ForEach-Object {  
[pscustomobject] @{  
PSTypeName=‘Wibble’  
Colour = $Name[$($_)]  
Date = $Date[$($_)]  
Message = “Something random $\_”  
}  
}  
$test.pstypenames[/pre]

Whilst this does work:  
[pre]  
$test2 = [pscustomobject] @{  
PSTypeName=‘Wibble’  
Colour = $Name[$(0)]  
Date = $Date[$(0)]  
Message = “Something random $\_”  
}  
$test2.pstypenames[/pre]

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [March 21, 2019, 7:53am UTC](https://forums.powershell.org/t/custom-types-and-pstypenames-behavior/12222/4 "2019-03-21T07:53:53Z")

</div>

PSTypenames is a hidden property that’s not as easily-found and not even in the same form as you declare it in that syntax. It receives special treatment, for convenience. That gets mapped to `$test2.PSObject.TypeNames`, which you’ll find is actually a _collection_ of different values (you can even `.Add()` a new value in if you like after creating the object). `PSTypeNames` is a shortcut specifically for `PSCustomObject` to allow you to declare the primary type name to use when creating it. Otherwise you wouldn’t be able to, and would always have to add it after creating the object.

&nbsp;

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/custom-types-and-pstypenames-behavior/12222/5 "2024-05-16T20:33:10Z")

</div>


