12

I often find that I have to surround a command in parentheses and then use the property access 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?

14

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 (Update: This does work in PowerShell v3.):

(Get-PSProvider).Drives

However, this will return the drives for all providers:

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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