2

I would like to get/select all items inside the curly brackets. I've tried to select only the property and than select evything but it doesnt give the wanted result.

Cmdlet

Get-ADUser -filter * -SearchBase "OU=test2OU,DC=corp-auryn,DC=serverlabs,DC=be" |select Propertynames |select *

Photo of Powershell cmdlet and output

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 8
    Change `Select PropertyName |Select *` to `Select -ExpandProperty PropertyNames` – Mathias R. Jessen Jun 08 '21 at 12:57
  • As an aside: the `{...}` is the PowerShell's output formatting system's way of representing property values that are collections (arrays), which can be confusing, given that array literals in source code are created with `@(...)` or just `,`-separated elements. – mklement0 Jun 09 '21 at 16:03

1 Answers1

2

Mathias R. Jessen has provided the crucial pointer:

In order to have Select-Object extract only a property value from its input objects, that property's name must be passed to the -ExpandProperty parameter.

  • By default, with the (positionally implied) -Property parameter, even if you pass just one property name, that property's value is copied to a property of the same name in the output object, as evidenced by your output: a [pscustomobject] instance with a single property, Propertynames, was emitted.

Additionally, if the property values happen to be arrays, their elements are enumerated.

  • This enumeration does not happen with -Property.

Note that this means that when the name of an array-valued property is passed to -ExpandProperty and there are multiple input objects, a flat array of values is returned, which is the concatenation of the individual property-value arrays[1], so that the input-object boundaries are lost:

A simple example:

[pscustomobject] @{ foo = 1, 2 }, [pscustomobject] @{ foo = 3, 4 } |
  Select-Object -ExpandProperty foo

The above outputs flat array 1, 2, 3, 4.


Note that member-access enumeration behaves the same way - except that it requires the input collection to be collected in memory in full first:

# Except for not *streaming* the input objects, the result
# is the same as above:
([pscustomobject] @{ foo = 1, 2 }, [pscustomobject] @{ foo = 3, 4 }).foo

[1] Since the elements of a property that contains a collection - which or may not technically be an array - are enumerated to get the result, you'll always end up with regular PowerShell array, of type [object[]].

mklement0
  • 382,024
  • 64
  • 607
  • 775