I'm adding a class related to software packages. Here is a barebones version for the purposes of this question:
class Package {
$Name; $Latest; $Prerelease
[string]ToString(){ return $this.Name }
}
$test = [Package]::new()
$test.Name = 'Example'
$test.Latest = '0.1'
When I perform the command $test
or $test | Format-Table
, I want the $Prerelease property to be hidden unless it's been given a value.
And, while this next part isn't a requirement, I also want it to be exposed when something like Format-Table -Force
is used.
I swear I've witnessed other objects in PowerShell behave similarly, but I haven't been able to recall what they were. If this isn't doable for an object made with pure PowerShell, I wouldn't mind something using C# via Add-Type
.
P.S. In regards to suggesting an alternative, bear in mind I want quoting the object to output the $Name property as it does in the class I posted above, e.g. "$test"
Prerelease
property unless you have a value for it)"$test"
you just get the $Name property. Is that doable with a PSCustomObject? Additionally, I'm wanting this to pipe into another command that would require the class for its InputObject parameter. I could rename the $Name property to something fancy and use ValueFromPipelineByPropertyName instead, but this isn't ideal.