How can I get a property of an object in powershell without using parentheses?
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?
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