Chapter 14. PowerShell type formatting

 

Adam Driscoll

Type formatting is used to define how a .NET type will be displayed on the PowerShell command line or host. Without the use of formatting a large number of .NET types would prove difficult to read. This chapter focuses on defining a custom formatting file for the PrintSystemJobInfo class. This class defines the print jobs that are in progress on local and remote print servers. The following script outputs all the print jobs on the local machine:

Add-Type –AssemblyName "System.Printing"

foreach($pq in (New-Object -TypeName
   System.Printing.PrintServer).GetPrintQueues())
{
    $pq.Refresh()
    $pq.GetPrintJobInfoCollection()
}

The script creates a new PrintServer object, iterates over the print queues on the system, and then returns the print jobs for each of the print queues. Running the script yields output similar to what’s shown here:

HostingPrintServer         : System.Printing.PrintServer
HostingPrintQueue          : System.Printing.PrintQueue
JobName                    : Print System Document
IsRetained                 : False
IsUserInterventionRequired : False
IsBlocked                  : False
IsDeleted                  : False
IsPaperOut                 : False
IsOffline                  : False
IsPrinting                 : False
...

Creating a formatting file

View definitions

Defining table headers

Conditional row entries

Grouping

Custom controls

Putting it together

Loading formatting data

Summary

About the author