Friday, July 20, 2012

Select Object Expand Property

Many times over the last year, I have wanted to get a specific property of an object, but I didn't have the exact object, just results from a cmdlet like Get-Acl.  So I would wrap the rest of the code in parentheses and write . and the property name.  Not my favorite thing to do.  Example:
(Get-Acl p:).access


I recently learned about Select-Object having an -ExpandProperty.  This means that instead of the parentheses, I can pipe to Select-Object -ExpandProperty, like so:
Get-Acl p: | select-object -expandproperty access


While it takes more words, I like it because it preserves the forward flow and means I don't have to go back and add parentheses.  This is a simple case, but I hope it illustrates my point.

No comments:

Post a Comment