Create Calculated Properties in PowerShell with Select-Object, Format-Table, and Format-List
PowerShell offers a range of cmdlets and functionalities that allow you to format and manipulate
data in countless ways. One such feature is the ability to create calculated properties using
Select-Object
, Format-Table
, and Format-List
. Calculated properties allow you to create custom
objects, enabling more effective data management and presentation.
Using Select-Object for Calculated Properties
The Select-Object
cmdlet is primarily used to select specific properties of an object but also
allows you to create new, calculated properties. To create a calculated property, use the following
syntax, where PropertyName
is the name or label of the new property:
1Select-Object -Property @{Name='PropertyName'; Expression={$_.Property}}
Using Format-Table or Format-List for Calculated Properties
The Format-Table
and Format-List
cmdlets can format the output as a table or list, and like
Select-Object
, they allow you to create calculated properties. To create a calculated property,
use the following syntax, where PropertyName
is the label or name of the new property:
1Format-Table -Property @{Label='PropertyName'; Expression={$_.Property}}
2
3Format-List -Property @{Label='PropertyName'; Expression={$_.Property}}
History of Calculated Properties
In PowerShell version 1.0, the Select-Object
cmdlet only supported Name for the hash table key
name of calculated properties, and Format-Table
and Format-List
only supported Label. In
PowerShell version 2.0, Select-Object
was updated to support Label, and Format-Table
and
Format-List
were updated to support Name. They now support both for backward compatibility and
consistency.
1Get-WmiObject -Class Win32_LogicalDisk |
2Select-Object -Property DeviceID, @{Label='FreeSpaceGB'; Expression={"{0:N2}" -f ($_.FreeSpace / 1GB)}}
3
4Get-WmiObject -Class Win32_LogicalDisk |
5Select-Object -Property DeviceID, @{Name='FreeSpaceGB'; Expression={"{0:N2}" -f ($_.FreeSpace / 1GB)}}
6
7Get-WmiObject -Class Win32_LogicalDisk |
8Format-Table -Property DeviceID, @{Label='FreeSpaceGB'; Expression={"{0:N2}" -f ($_.FreeSpace / 1GB)}}
9
10Get-WmiObject -Class Win32_LogicalDisk |
11Format-Table -Property DeviceID, @{Name='FreeSpaceGB'; Expression={"{0:N2}" -f ($_.FreeSpace / 1GB)}}
Scenario: Identify Classic Application Insights using Azure PowerShell
This scenario requires an Azure subscription and the Az PowerShell module. For installation instructions, see How to install Azure PowerShell or use the Az PowerShell module from Azure Cloud Shell.
You must identify classic Application Insights using Azure PowerShell with an additional property
named Type. The Type property should display Workspace-based
if the IngestionMode
property is equal to LogAnalytics
or Classic
if the IngestionMode property is equal to
ApplicationInsights
. Here’s how to achieve this with a calculated property:
1Get-AzApplicationInsights |
2Format-List -Property Name, IngestionMode, WorkspaceResourceId,
3 @{label='Type';expression={
4 if ($_.IngestionMode -eq 'LogAnalytics') {
5 'Workspace-based'
6 } elseif ($_.IngestionMode -eq 'ApplicationInsights') {
7 'Classic'
8 }
9 }}
Summary
PowerShell’s Select-Object
, Format-Table
, and Format-List
cmdlets are versatile and powerful,
allowing you to create calculated properties to manipulate and present data efficiently. Effectively
utilizing these cmdlets can produce more readable output and aid in data analysis and management
tasks.
Whether you format the display of process information, calculate disk space, or perform other data manipulation, leveraging calculated properties can enhance your PowerShell experience. Experiment with different expressions and formatting options to explore these cmdlets' extensive possibilities.