Question

I've written a cmdlet that returns a custom object, and I'm looking to try and have the data nicely formatted by default, without resorting to | format-table -autosize.

In essense, the object has three properties: two strings, and one int. One of the strings I know is never going to be more than 20 chars wide, but the other string could be over 100 chars.

At the moment, Powershell is auto-formatting the short column width at about 60 chars and the long column is taking the rest of the screen at 30 chars or so, so I end up with output that looks, by default, something like this:

FileName                                  LineNum Text
--------                                  ------- ---- 
test.txt                                       20 The cow had a dog ... 
rabbit.txt                                     17 It were a dark an ... 
cow.log                                        3  All work and no p ...

The object is simply created and written out as a PSObject() as:

var o = new PSObject();
o.Members.Add(new PSNoteProperty(fieldName, value));
[...]

this.WriteObject(o);

Ideally I'd like to remove that huge gap in the output. I can't see anything looking in the MSDN for PSNoteProperty, nor PSObject.Members.Add() that suggests a cmdlet can do this though.

Was it helpful?

Solution

If you want it to do that by default, you'll need to create a custom object type and a then create the format xml for that type:

http://msdn.microsoft.com/en-us/library/gg580944(v=vs.85).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top