I’ve been working on a PowerShell product lately – details to come – and spent the last 90 minutes ripping my locks out trying to figure out why one (and only one) of my PowerShell format definitions was not being applied. 

The PowerShell trace-command cmdlet helped me figure out what was happening during the view binding.  This handy little guy causes PowerShell internals to spit out a trace from specific subsystems – the one I’m interested in is named FormatViewBinding:

trace-command {dir|ft} -name FormatViewBinding -pshost

The trace output provided the clue I needed - the type I want to format is named VSDTE.PropertyModel.ShellColorableItem:

1 vs:\settings\texteditor\colors> trace-command {dir|ft} -name FormatViewBinding -pshost 2 FormatViewBinding Information: 0 : FINDING VIEW Table TYPE: VSDTE.PropertyModel.ShellColorableItem 3 FormatViewBinding Information: 0 : NOT MATCH Table NAME: Counter TYPE: Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet 4 FormatViewBinding Information: 0 : NOT MATCH Table NAME: Counter TYPE: Microsoft.PowerShell.Commands.GetCounter.CounterFileInfo 5 FormatViewBinding Information: 0 : NOT MATCH List NAME: System.Xml.XmlElement#http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd#IdentifyResponse 6 FormatViewBinding Information: 0 : NOT MATCH Table NAME: Microsoft.WSMan.Management.WSManConfigElement TYPE: Microsoft.WSMan.Management.WSManConfigElement 7 FormatViewBinding Information: 0 : NOT MATCH Table NAME: Microsoft.WSMan.Management.WSManConfigContainerElement TYPE: Microsoft.WSMan.Management.WSManConfigContainerElement 8 FormatViewBinding Information: 0 : NOT MATCH Table NAME: Microsoft.WSMan.Management.WSManConfigLeafElement TYPE: Microsoft.WSMan.Management.WSManConfigLeafElement 9 FormatViewBinding Information: 0 : NOT MATCH Table NAME: Microsoft.WSMan.Management.WSManConfigLeafElement#InitParams TYPE: Microsoft.WSMan.Management.WSManConfigLeafElement#InitParams 10 FormatViewBinding Information: 0 : NOT MATCH Table NAME: Microsoft.WSMan.Management.WSManConfigContainerElement#ComputerLevel TYPE: Microsoft.WSMan.Management.WSManConfigContainerElement#ComputerLevel 11 ... 12 FormatViewBinding Information: 0 : NOT MATCH List NAME: System.Object[] 13 FormatViewBinding Information: 0 : NOT MATCH Table NAME: ColorableItemAsChildren TYPE: 14 VSDTE.PropertyModel.ShellColorableItem 15 16 FormatViewBinding Information: 0 : NOT MATCH List NAME: ColorableItemAsChildren 17 FormatViewBinding Information: 0 : No applicable view has been found 18
See how my type name shows up on line 14 all by itself?  See how the rest of the type names don’t?  Yep…

Looking at my format ps1xml file revealed some whitespace inside of my <ViewSelectedBy><TypeName/> element:

1 <ViewDefinitions> 2 <View> 3 <Name>ColorableItemAsChildren</Name> 4 <ViewSelectedBy> 5 <TypeName> 6 PStudioShell.DTE.PropertyModel.ShellColorableItem 7 </TypeName> 8 </ViewSelectedBy> 9 ...

Removing this whitespace from this element fixed the problem – my format definition was matched and applied as I expected:

1 vs:\settings\texteditor\colors> trace-command {dir|ft} -name FormatViewBinding -pshost 2 FormatViewBinding Information: 0 : FINDING VIEW Table TYPE: VSDTE.PropertyModel.ShellColorableItem 3 FormatViewBinding Information: 0 : NOT MATCH Table NAME: Counter TYPE: Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSampleSet 4 FormatViewBinding Information: 0 : NOT MATCH Table NAME: Counter TYPE: Microsoft.PowerShell.Commands.GetCounter.CounterFileInfo 5 ... 6 FormatViewBinding Information: 0 : MATCH FOUND Table NAME: ColorableItemAsChildren TYPE: PStudioShell.DTE.PropertyModel.ShellColorableItem 7 FormatViewBinding Information: 0 : An applicable view has been found 8

The worst part?  That whitespace was added automagically by the VS XML editor.

I realize that in an XML text node whitespace can be important, but it isn’t here – typenames can’t contain whitespace.  This feels like a silly bug, not behavior I would expect or tolerate.

Anyway, fair warning to my fellow PowerShell hackers!

Update

I’ve logged this on Microsoft Connect as a bug; please upvote it here.