8

Can somebody explain to me what is the difference between dot notation and select-object in Powershell? How these two methods of accessing properties differ internally?

I've noticed that running (ls).name gives basically the same results as ls | select-object name however running ls | select-object name | export-csv foo.csv gives me proper csv file while trying (ls).name | export-csv foo.csv gives me the file with length. In both cases gettype() returns Object[]

1 Answer 1

6

The select-object cmdlet wraps the result in a new object. To see the differences (look at the Type) use the get-member cmdlet.

(ls).Name | get-member

and

ls | select-object Name | get-member
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I understand the difference now. It seems that (ls).name is equal to the ls | select-object -expandproperty name. Is it possible to use dot notation without expanding property?
No. Dot notation is reading the value of the property you specify, just like ls | % { $_.name } would, so it you want it as a property in a New Object, you have to use Select-Object or New-Object (in a foreach-loop).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.