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?
Are you sure you are going about this in a prudent way?
See these for possible edification:
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...
Est. reading time: 3 minutes
https://learningintheopen.org/2011/12/22/microsoft-powershell-list-all-the-properties-for-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…
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]
ta11ow
March 21, 2019, 7:53am
4
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.