PowerShell Tips for System Administrators: Format Views

Amazing View
Photo by Dave_B_

In two recent posts I went over the PowerShell formatting cmdlets and calculated properties. Today I’m going to cover Views. You’ll notice that when you display a normal object (such as the built-in FileInfo objects when you run dir) you don’t see all of its properties. This is because PowerShell is using a View, which is selected based on the type of object you’re outputting and which type of output is being used (table, list, or wide.)

The views are stored in XML files in the PowerShell directory which you can examine to see how the formatting is being performed. Not only that but you can add your own views and even override the default views used by PowerShell.

The standard views are stored in %systemroot%\System32\WindowsPowerShell\v1.0 in a files with the extension format.ps1xml. There are several files, but the one we’ll be looking at is FileSystem.format.ps1xml. This file holds the formatting for the various file system objects, such as FileInfo and DirectoryInfo. The section that defines the normal file system table output is this:

<View>
    <Name>children</Name>
    <ViewSelectedBy>
    <SelectionSetName>FileSystemTypes</SelectionSetName>
    </ViewSelectedBy>
    <GroupBy>
    <PropertyName>PSParentPath</PropertyName>
    <CustomControlName>FileSystemTypes-GroupingFormat</CustomControlName>
    </GroupBy>
    <TableControl>
    <TableHeaders>
    <TableColumnHeader>
    <Label>Mode</Label>
    <Width>7</Width>
    <Alignment>left</Alignment>
    </TableColumnHeader>
    <TableColumnHeader>
    <Label>LastWriteTime</Label>
    <Width>25</Width>
    <Alignment>right</Alignment>
    </TableColumnHeader>
    <TableColumnHeader>
    <Label>Length</Label>
    <Width>10</Width>
    <Alignment>right</Alignment>
    </TableColumnHeader>
    <TableColumnHeader />
    </TableHeaders>
    <TableRowEntries>
    <TableRowEntry>
    <Wrap />
    <TableColumnItems>
    <TableColumnItem>
    <PropertyName>Mode</PropertyName>
    </TableColumnItem>
    <TableColumnItem>
    <ScriptBlock> [String]::Format("{0,10} {1,8}", $_.LastWriteTime.ToString("d"), $_.LastWriteTime.ToString("t")) </ScriptBlock>
    </TableColumnItem>
    <TableColumnItem>
    <PropertyName>Length</PropertyName>
    </TableColumnItem>
    <TableColumnItem>
    <PropertyName>Name</PropertyName>
    </TableColumnItem>
    </TableColumnItems>
    </TableRowEntry>
    </TableRowEntries>
    </TableControl>
</View>

If you’re not used to XML this may appear a bit daunting, but you can get a quick sense of how it works. There are four main sections of the file.

  1. indicates which object types are used by this view. In this case, the name FileSystemTypes refers to another section in the file that lists the types. That section is . The individual type names can be listed instead, but this reference makes the code shorter.
  2. tells the table of files to be grouped by thier parent path. This also refers to another section of the file called .
  3. sets up not only the titles that appear on the table columns, but also their width and alignment.
  4. the actual values to show in the table. Three of the colums just pull out a property by name, but the LastModifiedTime uses PowerShell code to format the date in a particular way. Any PowerShell code can be used which gives a great deal of flexibility.

There are a lot of possibilities for how to create views and the documentation is a bit limited. To learn about formatting it’s best to look at the included format files to see how things are done.

To make a custom view you will need to create your own format.ps1xml files since you can’t edit the built in ones. If there is more than one view for an object then the first one found will be used. Otherwise they can be selected by name using the -View parameter of the format cmdlets. Once you have a file created you import the formats using the Format-UpdateData cmdlet. As an example of a new format, I’ve create a format file that adds a CreationTime column in addition to LastWriteTime.
Download the file, save it with the extension .format.ps1xml and run the following command:

Format-UpdateData -PrependPath [filename]

Now when you list the contents of a directory you will see the new column. Alternately you could have used -AppendPath to put the file after the built in formats and then you would need to use the view by name:

dir | Format-Table -View MyFormat

To make the custom format persistent between sessions, add the Format-UpdateData command to your PowerShell profile. One of the principles of PowerShell is the ability for you to configure it to your tastes and Views are a very powerful, if somewhat complex, way to get just want you want.


Need help using formats (or anything else) in PowerShell? Post a question in our discussion forums to ask one of our engineers.