I often find that I have to surround a command in parentheses and then use the property access to operator (dot-syntax) to get the value of a property. This is rather annoying since I have to go back to the beginning of the line when I'd rather just keep typing where I am. It is especially annoying when I am in the middle of a larger set of piped commands.

Example

If I have the following command

Get-PSProvider FileSystem

and I want to get the Drives property, I would have to surround the whole command in parentheses first:

(Get-PSProvider FileSystem).Drives

Is there a faster way to get the value of a single property?

link|improve this question
feedback

1 Answer

up vote 5 down vote accepted

You can use Select-Object -ExpandProperty <property name>. This can be shortened using the alias select and only typing part of the property name:

Get-PSProvider FileSystem| select -exp Drives

An additional benefit to this method is that you can access a single property for multiple objects.

This will not return anything:

(Get-PSProvider).Drives

However, this will return the drives for all providers:

Get-PSProvider| select -exp Drives
link|improve this answer
Very interesting. Thank you for the correction. – soandos Dec 27 '11 at 3:05
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.