Reputation: 715
I am developing a Powershell Cmdlet using C#, and I was wondering if there's a way of controlling the formatting of the objects I write to Powershell in the Cmdlet itself. Basically, I have objects with too many properties to display easily on the screen, and my Cmdlet is sending those to Powershell with WriteObject()
I would like users of my cmdlets to be able to run them and, in the Powershell console, read the data returned as they would have done using the command prompt. Unfortunately, the number of properties Powershell is trying to fit in columns means most are truncated, and when I add more it gives each property its own line which is worse.
I've seen things which allow the user to format the data appropriately, but nothing that allows the developer to set a default. Basically what I want is something like an Attribute I can apply to each property of the objects being pumped through to Powershell which tells Powershell whether to display each property or not (assuming the user has set no other formatting options).
(I've tried making them public fields instead of properties too, and Powershell still shows them)
Upvotes: 6
Views: 3051
Reputation: 16606
I'm assuming you're using PowerShell 2.0.
If your module is called MyModule
, you can create a MyModule.Format.ps1xml
file alongside it to describe what properties are displayed by default by Format-List
, Format-Table
, etc. See Get-Help about_Format.ps1xml
and Formatting File Overview for more information.
To link your formatting file to your module, you would create a module manifest and define the FormatsToProcess
item. The New-ModuleManifest
cmdlet can get you started with this.
Upvotes: 10