Update-FormatData

In the last article we learnt how to extend a Type with Update-TypeData today we will learn how to get the output to look just the way we want using Update-FormatData

CMDLET Name : Update-FormatData

What it does: This cmdlet lets you update the formatting data without restarting Windows PowerShell.

When you run Get-Service without any parameters we get this :

PS C:\> Get-Service

 

Status   Name               DisplayName                          

——   —-               ———–                          

Stopped  AeLookupSvc        Application Experience               

Stopped  ALG                Application Layer Gateway Service    

Stopped  AppIDSvc           Application Identity                            

 

there are only 3 properties namely : Status, Name, DisplayName

displayed in a Table format.

let’s use a select statement to display the same 3 properties above :

image

the output is still a table but notice the padding on the left and also the huge gap between the name & displayname columns.

using the select statement once more with 5 properties:

PS C:\> Get-Service | select Name,DisplayName,Status,DependentServices,RequiredServices

 

 

Name              : AeLookupSvc

DisplayName       : Application Experience

Status            : Stopped

DependentServices : {}

RequiredServices  : {}

 

Name              : ALG

DisplayName       : Application Layer Gateway Service

Status            : Stopped

DependentServices : {}

RequiredServices  : {}

 

this time we get a list instead of a table. So how does PowerShell know to display data in these specific formats based on the input. ?

The formatting data for windows PowerShell is stored in files with a an extension *format.ps1xml

To get a list of PowerShell format files we run:

PS C:\> dir -Path c:\windows\system32\windowspowershell -filter *format.ps1xml -recurse | select FullName

 

FullName                                                                                                                                                                                                                              

——–                                                                                                                                                                                                                              

C:\windows\system32\windowspowershell\v1.0\Certificate.format.ps1xml                                                                                                                                                                   C:\windows\system32\windowspowershell\v1.0\Diagnostics.Format.ps1xml                                                                                                                                                                   

C:\windows\system32\windowspowershell\v1.0\DotNetTypes.format.ps1xml                                                                                                                                                                   

C:\windows\system32\windowspowershell\v1.0\Event.Format.ps1xml                                     

 

Since we get a large list I have truncated the output above. In order to see the formatting data for Get-Service we need to first determine the Type of objects produced by Get-Service.

How do we do that? Get-Member of course:

PS C:\> Get-Service | Get-Member

 

   TypeName: System.ServiceProcess.ServiceController

 

so Get-Service produces objects of type  : System.ServiceProcess.ServiceController

The formatting data for System.ServiceProcess.ServiceController should be stored in one of those format.ps1xml files but how do know which one? we employ a pattern search:

PS C:\> dir -Path c:\windows\system32\windowspowershell -filter *format.ps1xml -recurse | Select-String -Pattern System.ServiceProcess.ServiceController

 

windows\system32\windowspowershell\v1.0\DotNetTypes.format.ps1xml:766:                <TypeName>System.ServiceProcess.ServiceController</TypeName>

windows\system32\windowspowershell\v1.0\DotNetTypes.format.ps1xml:1092:            <Name>System.ServiceProcess.ServiceController</Name>

windows\system32\windowspowershell\v1.0\DotNetTypes.format.ps1xml:1094:                <TypeName>System.ServiceProcess.ServiceController</TypeName>

 

ah so our service type can be found in a file named: DotNetTypes.format.ps1xml located at:

c:\windows\system32\windowspowershell\v1.0

The service type appears thrice on the file above: Lines 766, 1092, & 1094

Lets open the file in PowerShell ISE :

image

so the view definition for System.ServiceProcess.ServiceController starts on line 763 and ends on line 796.

Line 771:           Contains format definition for the first property Status which is 8 characters wide.

Line 774:           Name column that is 18 characters wide. & lastly

Line 777:           DisplayName which is the widest of the 3 columns at 38 characters. This is because the displayname column contains entries that are longer than the values contained in the other 2 columns.

Lets go to the second instance of System.ServiceProcess.ServiceController on line 1092.

image

here we can see the format definitions for a list view with a total of 9 properties.

Let’s create a custom format file to display 4 properties by default in a table format :

Name,DisplayName,Status,DependentServices

Note :  Never modify the built-in files by Microsoft because these files are digitally signed and when you modify them that signature breaks and you may not be able to run PowerShell.

 

Our custom file will be stored in: c:\temp\GetServiceCustomFormat.Format.Ps1xml

instead of hand coding all the data we can make our lives a little easier by copying lines 763 to 796 from : C:\windows\system32\windowspowershell\v1.0\DotNetTypes.format.ps1xml

since we need this in an xml format we need to prepend the data with following line:

also view definitions must be enclosed within the following tags :

 

So our file should look like this:

image

Since we want to add a new column we insert a new tablecolumnheader entry at line 20 and set the width at 20 characters wide and a new tableColumnItem after line 36 with the columnname as DependentServices

Our completed custom format file should look like this:

 

Right, it is time now to import this into our session:

PS C:\> Update-FormatData -PrependPath C:\temp\GetServiceCustomFormat.Format.ps1xml

 

PS C:\> Get-Service

 

Status   Name               DisplayName                            DependentServices  

——   —-               ———–                            —————–  

Stopped  AeLookupSvc        Application Experience                 {}                 

Stopped  ALG                Application Layer Gateway Service      {}                 

Stopped  AppIDSvc           Application Identity                   {}                 

Running  Appinfo            Application Information                {}                 

Running  AppMgmt            Application Management                 {}                 

Stopped  AppReadiness       App Readiness                          {}                 

Stopped  AppXSvc            AppX Deployment Service (AppXSVC)      {}                 

Stopped  aspnet_state       ASP.NET State Service                  {}                 

Running  AudioEndpointBu… Windows Audio Endpoint Builder         {Audiosrv}         

Running  Audiosrv           Windows Audio                          {}                 

Stopped  AxInstSV           ActiveX Installer (AxInstSV)           {}                 

Stopped  BDESVC             BitLocker Drive Encryption Service     {}                 

Running  BFE                Base Filtering Engine                  {WdNisSvc, WdNisD…

Running  BITS               Background Intelligent Transfer Ser… {}                  

 

Great so our new column shows up in the default view. If you notice some of the entries ex: Name – BFE , the dependentservices column entry is cutoff towards the end. That’s because we set our column width to be 20 but the actual column entry is more than 20 characters wide.If you would like you can modify the width settings to whatever you like but depending on your screen size the output may or may not display.

Just like Update-TypeData the view definitions created by the Update-FormatData  cmdlet is valid for the current session only. You would have to either re-run update-formatdata or dot source it in your profile.

Lets do one more example this time as promised we will add custom format definitions for :

System.Management.ManagementObject#root\cimv2\Win32_PhysicalMemory

I wont go over all the details but here is the output before adding the custom definitions:

PS C:\> Get-WmiObject -Class Win32_PhysicalMemory

 

 

__GENUS              : 2

__CLASS              : Win32_PhysicalMemory

__SUPERCLASS         : CIM_PhysicalMemory

__DYNASTY            : CIM_ManagedSystemElement

__RELPATH            : Win32_PhysicalMemory.Tag=”Physical Memory 0″

__PROPERTY_COUNT     : 30

__DERIVATION         : {CIM_PhysicalMemory, CIM_Chip, CIM_PhysicalComponent, CIM_PhysicalElement…}

__SERVER             : VM1

__NAMESPACE          : root\cimv2

__PATH               : \\VM1\root\cimv2:Win32_PhysicalMemory.Tag=”Physical Memory 0″

BankLabel            : RAM slot #0

Capacity             : 2147483648

Caption              : Physical Memory

CreationClassName    : Win32_PhysicalMemory

DataWidth            : 32

Description          : Physical Memory

DeviceLocator        : RAM slot #0

FormFactor           : 8

HotSwappable         :

InstallDate          :

InterleaveDataDepth  :

InterleavePosition   :

Manufacturer         :

MemoryType           : 2

Model                :

Name                 : Physical Memory

OtherIdentifyingInfo :

PartNumber           :

PositionInRow        :

PoweredOn            :

Removable            :

Replaceable          :

SerialNumber         :

SKU                  :

Speed                :

Status               :

Tag                  : Physical Memory 0

TotalWidth           : 32

TypeDetail           : 512

Version              :

PSComputerName       : VM1

 

and now here it is after importing our custom format file.

PS C:\> Update-FormatData -PrependPath C:\temp\Win32PhysicalMemoryCustom.Format.ps1xml

 

PS C:\> Get-WmiObject -Class Win32_PhysicalMemory

 

ComputerName   BankLabel     DataWidth  TotalWidth CapacityMB  MemoryType

                                                               String   

————   ———     ———  ———- ———-  ———-

VM1         RAM slot #0   32         32         2048        DRAM     

VM1         RAM slot #1   32         32         1024        DRAM     

VM1         RAM slot #2   32         32         512         DRAM     

VM1         RAM slot #3   32         32         256         DRAM     

VM1         RAM slot #4   32         32         128         DRAM     

VM1         RAM slot #5   32         32         32          DRAM     

VM1         RAM slot #6   32         32         16          DRAM      

 

Nice and clean!

You can achieve pretty much the same result using the  Update-TypeData cmdlet.

For more details check out my previous post: Update-TypeData

You can make this even easier if you have the WMIX modules on your system.

Check out my previous post’s on WMIX for more details

WMIX – Part1

WMIX – Part2

Once you have the WMIX module imported instead of Win32_Physicalmemory you can just type:

 PS C:\> Get-PhysicalMemory

 

ComputerName   BankLabel     DataWidth  TotalWidth CapacityMB  MemoryType

                                                               String   

————   ———     ———  ———- ———-  ———-

VM1         RAM slot #0   32         32         2048        DRAM     

VM1         RAM slot #1   32         32         1024        DRAM     

VM1         RAM slot #2   32         32         512         DRAM     

VM1         RAM slot #3   32         32         256         DRAM     

VM1         RAM slot #4   32         32         128         DRAM     

VM1         RAM slot #5   32         32         32          DRAM     

VM1         RAM slot #6   32         32         16          DRAM      

 

Exactly identical output!

You can go one step further and define an alias so that our command becomes even shorter:

PS C:\> Set-Alias -Name GMEM -Value Get-PhysicalMemory

 

PS C:\> gmem

 

ComputerName   BankLabel     DataWidth  TotalWidth CapacityMB  MemoryType

                                                               String   

————   ———     ———  ———- ———-  ———-

VM1         RAM slot #0   32         32         2048        DRAM     

VM1         RAM slot #1   32         32         1024        DRAM     

VM1         RAM slot #2   32         32         512         DRAM     

VM1         RAM slot #3   32         32         256         DRAM     

VM1         RAM slot #4   32         32         128         DRAM     

VM1         RAM slot #5   32         32         32          DRAM     

VM1         RAM slot #6   32         32         16          DRAM      

 

 

Download Attachments

  • zip ps1xml files
    Custom PS1XML format files for Get-Service and Win32_Physicalmemory
    File size: 1 kB Downloads: 273

Leave a Comment Yourself

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">