Property vs. ExpandProperty in PowerShell

Property vs. ExpandProperty in PowerShell

Windows PowerShell is mainly centralized in displaying and generating objects. These objects are supported with specific details called properties.

This article will discuss what properties are and differentiate them from expanded properties.

Property vs. ExpandProperty in PowerShell

It may not be very clear when explain the difference between properties and expanded properties, so let us take an example that will help us visualize it further.

Let us start with getting the properties of the Get-Process command by running the code below.

Example Code:

Get-Process | Format-List -Property *

Upon running the command above, PowerShell will generate a structure of collections of information associated with an object. The output below is a few of the many properties of the Get-Process command.

Output:

Name                       : ApplicationFrameHost
Id                         : 13248
...
Modules                    : {System.Diagnostics.ProcessModule (ApplicationFrameHost.exe),
                             System.Diagnostics.ProcessModule (ntdll.dll), System.Diagnostics.ProcessModule
                             (KERNEL32.DLL), System.Diagnostics.ProcessModule (KERNELBASE.dll)...}

Let us get one of the output properties above and examine them further.

Example Code:

Get-Process | Select-Object -Property Modules

Output:

Modules
------
{System.Diagnostics.ProcessModule (ApplicationFrameHost.exe), System.Diagnostics.ProcessModule (ntdll.dll), System.D...

As we can see, one of the Modules property displayed as output; however, it is not currently readable. The data in the Modules property above is placed in an ArrayList.

So, how do we make this property more readable? We expand the property using the ExpandProperty parameter.

The ExpandProperty parameter is used to look into the sub-properties of a PowerShell object’s primary properties. PowerShell generates a multi-level structure to define the objects, which are properties and expanded properties.

Example Code:

Get-Process | Select-Object -ExpandProperty Modules

Output:

   Size(K) ModuleName                                         FileName
   ------- ----------                                         --------
        84 ApplicationFrameHost.exe                           C:\WINDOWS\system32\ApplicationFrameHost.exe
        <SNIP>
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn