The -property option specifies the names of the properties that are to be sent down further the pipeline.
If -property is used, the select-objectcmdLet creates a new PSCustomObject with the stated property names and the respective values copied from the received objects. select-object then sends the new PSCustomObject further down the pipeline.
In the following simple pipeline, the output of get-process (which are System.Diagnostic.Process objects) are just piped into a forEach-object cmdLet in order to print the object's type with GetType(). As expected, it prints System.Diagnostics.Process:
-expandProperty propertyName expands array or object properties into its elements.
The following example demonstrates how -expandProperty increaes the number of objects in a pipeline:
#
# An array with three elements is created.
#
# Each element is a psCustomObject with the two note properties
# key_1 and key_2.
#
# Note: key_1 of the last element in the array contains an array.
#
$ary =
[psCustomObject] @{ key_1 = 'val' ; key_2 = 'foo' },
[psCustomObject] @{ key_1 = 'another val' ; key_2 = 'bar' },
[psCustomObject] @{ key_1 = 'an', 'array', 'of', 'words'; key_2 = 'baz' }
#
# The array is sent to the select-object cmdLet, once with the -property
# and once with the -expandedProperties argument. The return values
# are captured in two variables:
#
$properties = $ary | select-object -property key_1
$expandedProperties = $ary | select-object -expandProperty key_1
#
# With -property, select-object just filters the properties from
# the object it receives from the pipeline. Thus, we still have
# three elements:
#
$properties.count
#
# 3
#
# However, when using -expandedProperties, select-object will
# turn each element of the value of the expanded property into
# its own object. Because the last element of $ary contains 4 elements,
# we now have 6 (=2+4) objects (strings) in the $expandedProperties
# Variable
$expandedProperties.count
#
# 6
#
# The second (last) element in $properties is still an array:
#
$properties[2]
#
# key_1
# -----
# {an, array, of, words}
#
# The second element $expandedProperties is the first element
# of the array that was assigned to key_1:
#
$expandedProperties[2]
#
# an
This hash table needs to have two keys: L (or label or name) whose value specifies the name of the added property and E (or expression) whose value will determine the name that is associated with the new property.
Note: the expression to be evaluated goes within curly braces, at least in the following example. In the expression, the object in the pipeline from which the values are taken is referred to by the PowerShell: the automatic variable $psItem ($_)[automatic variable $_):
[psCustomObject] @{ x = 7.1 ; y = 3.3 } ,
[psCustomObject] @{ x = 8.4 ; y = 3.6 } ,
[psCustomObject] @{ x = 6.9 ; y = 3.4 } |
select-object `
x,
y,
@{L = 'ratio'; E = { '{0,5:N2}' -f ($_.x / $_.y) } },
@{L = ' sum'; E = { '{0,4:N1}' -f ($_.x + $_.y) } }
#
# x y ratio sum
# - - ----- ----
# 7.1 3.3 2.15 10.4
# 8.4 3.6 2.33 12.0
# 6.9 3.4 2.03 10.3