scobi
scobi

Reputation: 14558

How to alter the formatting of a single member of my type in Powershell?

I have a set of C# classes that each have a ulong member or two. Powershell outputs these by default as 10-base numbers, but I'd like to show them as hex numbers with a 0x prefix. I'm trying to figure out how to do this right with .ps1xml files.

The first thing I tried was to add a types.ps1xml file that adds a script property with the same name as the one I want to replace. Works fine if I give it a different name (i.e. add a new member) but crashes the ISE if I use the same name as an existing one, like this:

<Members>
    <ScriptProperty>
        <Name>ForeignId</Name>
        <GetScriptBlock>'0x{0:x16}' -f $this.ForeignId</GetScriptBlock>
    </ScriptProperty>      
</Members>

I assume this is an infinite recursion issue.

Maybe what I really want to do is change the formatting! So I started looking at making a format.ps1xml and using <FormatString>, but now it seems I need to define the formatting completely. I must specify every member I want, and I must do it for all the views I might want. Well, that's a lot of work..

I just want to change the formatting of a single member without adding a new member. Is that possible?

Upvotes: 1

Views: 161

Answers (1)

mjolinor
mjolinor

Reputation: 68341

Ran across this a couple of days ago. Maybe useful?

http://poshoholic.com/2008/07/05/essential-powershell-define-default-properties-for-custom-objects/

Upvotes: 1

Related Questions