I used this article to write my first own Powershell Cmdlet and Snapin. And it works fine.
But I return a set of objects from my own data class, which has four properties and I want Powershell to display just one of these properties by default. So I used this part of the article to create this format file:
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>RemoteFile</Name>
<ViewSelectedBy>
<TypeName>MyFullNamespace.RemoteFileData</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader />
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Filename</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
and link it in the Snapin:
public override string[] Formats
{
get { return new string[] { "MyFormatFilename.ps1xml" }; }
}
But when I install the Snapin with installutil, use Add-PSSnapin and call my Cmdlet, all Properties of the objects are displayed.
What am I doing wrong?
Everything looks correct except that I’m not sure how it behaves with no column header label defined. Try adding this node instead of your empty one:
Also make sure the file
MyFormatFilename.ps1xmlis in the same dir with the snapin when it is being loaded via Add-PSSnapin. Also, probably a duh, but double check for typos in the type name specified in the<TypeName>element.Update: I tried your XML as listed above and it works for me. I copied it into Notepad2 and saved it to C:\temp\test.ps1xml then executed:
I would double check the full typename
instance.GetType().FullNameand also double check the contents of the format file. Make sure it is in the same dir that you registered the snapin from.