Powershell Custom Object Formatting

Today I would like to share some guidelines on how to apply formatting to custom powershell objects. There are many times when writing advanced functions, cmdlets and modules where a custom object is very useful, so let's start by creating an object type for our custom objects.

Creating a type is very simple, you just define the type as you would in C# and then add it:

add-type @'
namespace TestModule.CustomObject
{
    public class ExchangeServerMaintenanceMode
    {
        public string Name;
        public bool Enabled;
        public int ID;
    }
}
'@

I should also add here that Powershell 5 makes it even easier by allowing us to create classes directly!

Now that we have added the type of our custom object, lets create some objects!

$myuser = New-Object TestModule.CustomObject
$myuser.Id = "1"
$myuser.Name = "User1"
$myuser.Enabled = $false

Here, we have created a new object of type TestModule.CustomObject and we have stored in the "myuser" variable. Then we updated its properties.

Let's see what happens when we send the custom object to the Write-Output cmdlet:

The object is displayed in a table formatting. This is because the property names are short enough to allow the object to be displayed in a table formatting. Lets change the default formatting with a formatting file!

Formatting files are xml files that control the various options of how objects are formatted. This is an example of a small formatting file:

<?xml version="1.0" encoding="utf-16"?>
<Configuration>
<ViewDefinitions>
<View>
  <Name>myView</Name>
  <ViewSelectedBy>
    <TypeName>TestModule.CustomObject</TypeName>
  </ViewSelectedBy>
  <TableControl>
    <TableRowEntries>
      <TableRowEntry>
        <TableColumnItems>
          <TableColumnItem>
           <PropertyName>Name</PropertyName>
          </TableColumnItem>
          <TableColumnItem>
            <PropertyName>Enabled</PropertyName>
          </TableColumnItem>
          <TableColumnItem>
            <PropertyName>ID</PropertyName>
          </TableColumnItem>
        </TableColumnItems>
      </TableRowEntry>
    </TableRowEntries>
    <AutoSize/>
  </TableControl>
</View>
</ViewDefinitions>
</Configuration>

We have set the TypeName to "TestModule.CustomObject" in order to identify the object type to apply the formatting to and named this view as "myView". You can have many views and use them with the -View option of the formatting cmdlets.

In this formatting file, I have created a table view for our custom object and selected the properties to display. A list view would have a very similar structure.

You may also change the names of  the headers of the columns to short them out or make them more descriptive without changing the objects and much more. Don't forget to include the formatting file in the "FormatsToProcess" directive of your module manifest!

Popular posts from this blog

Domain Controller Machine Password Reset

Configuring a Certificate on Exchange Receive Connector

Running Multiple NGINX Ingress Controllers in AKS