I'm stumped by some of the behavior of the Select-Object cmdlet. Here is an example.
PS C:\> $object = get-item C:\windows\Temp
PS C:\> $time = $object.CreationTime
PS C:\> $time.GetType().FullName
System.DateTime
PS C:\> $result = Select-Object -InputObject $object -Property "CreationTime"
PS C:\> $result.GetType().FullName
System.Management.Automation.PSCustomObject
PS C:\>
PS C:\> $result.CreationTime.GetType().FullName
System.DateTime
Notice that CreationTime's type is System.DateTime but when I select it using Select-Object the returned object is of type System.Management.Automation.PSCustomObject. Its some new object that has CreationTime as its property.
Let's look at the help for Select-Object to explain this.
SYNOPSIS
Selects specified properties of an object or set of objects...
That's what I wanted, the property itself... not some object with the property. But if we read further...
DESCRIPTION
...If you use Select-Object to select specified properties, it copies the values of those properties from the input objects and creates new objects that have the specified properties and copied values.
I have no idea why that's useful, but it explains this object returned.
Using -ExpandProperty instead of -Property seems to give the property itself
PS C:\> $result2 = Select-Object -InputObject $object -ExpandProperty "CreationTime"
PS C:\> $result2.GetType().FullName
System.DateTime
By why does -ExpandProperty do this? Let's look at its help:
-ExpandProperty
Specifies a property to select... If the property contains an object, the properties of that object are displayed in the output.
In this case, the property is an object and we didn't get "the properties of that object" we only got the object itself.
Can someone tell me:
- Why does
-ExpandPropertyget the property-object itself when the help mentions expanding arrays and getting properties of the property-object (not the property-object itself)? This is where I am pretty confused. Am I reading this wrong? I feel like this needs another iteration of technical editing. - The object that gets returned with "-Property", is it useful for something?