PoshCode Logo PowerShell Code Repository

Select w/ subproperties by Dmitry Sotnikov 24 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1606"></script>download | new post

Allows to use dots to specify object subproperties in Select-Object.

For example:
Set-Process | Select-Object ProcessName, StartTime.DayOfWeek

This is the only changed behavior (properties with dots get evaluated as expressions) – everything else stays intact.

For more information see:
http://dmitrysotnikov.wordpress.com/2010/01/25/select-object-with-subproperties

  1. <#
  2.   This is a proxy function enhancing Select-Object by adding the
  3.   ability to use subproperties in the Property parameters.
  4.  
  5.   For example:
  6.   Set-Process | Select-Object ProcessName, StartTime.DayOfWeek
  7.  
  8.   This is the only changed behavior (properties with dots
  9.   get evaluated as expressions) - everything else stays intact.
  10.  
  11.   For more information see:
  12. http://dmitrysotnikov.wordpress.com/2010/01/25/select-object-with-subproperties
  13. #>
  14.  
  15. function Select-Object {
  16. [CmdletBinding(DefaultParameterSetName='DefaultParameter')]
  17. param(
  18.   [Parameter(ValueFromPipeline=$true)]
  19.   [System.Management.Automation.PSObject]
  20.   ${InputObject},
  21.  
  22.   [Parameter(ParameterSetName='DefaultParameter', Position=0)]
  23.   [System.Object[]]
  24.   ${Property},
  25.  
  26.   [Parameter(ParameterSetName='DefaultParameter')]
  27.   [System.String[]]
  28.   ${ExcludeProperty},
  29.  
  30.   [Parameter(ParameterSetName='DefaultParameter')]
  31.   [System.String]
  32.   ${ExpandProperty},
  33.  
  34.   [Switch]
  35.   ${Unique},
  36.  
  37.   [Parameter(ParameterSetName='DefaultParameter')]
  38.   [ValidateRange(0, 2147483647)]
  39.   [System.Int32]
  40.   ${Last},
  41.  
  42.   [Parameter(ParameterSetName='DefaultParameter')]
  43.   [ValidateRange(0, 2147483647)]
  44.   [System.Int32]
  45.   ${First},
  46.  
  47.   [Parameter(ParameterSetName='DefaultParameter')]
  48.   [ValidateRange(0, 2147483647)]
  49.   [System.Int32]
  50.   ${Skip},
  51.  
  52.   [Parameter(ParameterSetName='IndexParameter')]
  53.   [ValidateRange(0, 2147483647)]
  54.   [System.Int32[]]
  55.   ${Index})
  56.  
  57. begin
  58. {
  59.  try {
  60.      $outBuffer = $null
  61.      if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
  62.      {
  63.          $PSBoundParameters['OutBuffer'] = 1
  64.      }
  65.      
  66.      #region: Dmitry Sotnikov: substitute dotted properties with expressions
  67.      if ($Property -ne $null) {
  68.       # Iterate through properties and substitute those with dots
  69.       $NewProperty = @()
  70.       foreach ( $prop in $Property ) {
  71.        if ($prop.GetType().Name -eq 'String') {
  72.         if ($prop.Contains('.')) {
  73.          [String] $exp = '$_.' + $prop
  74.          $prop = @{Name=$prop; Expression = {Invoke-Expression ($exp)}}
  75.         }
  76.        }
  77.        $NewProperty += $prop
  78.       }
  79.       $PSBoundParameters['Property'] = $NewProperty
  80.      }
  81.      #endregion
  82.      
  83.      $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Select-Object',
  84.          [System.Management.Automation.CommandTypes]::Cmdlet)
  85.      $scriptCmd = {& $wrappedCmd @PSBoundParameters }
  86.      $steppablePipeline =
  87.          $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
  88.      $steppablePipeline.Begin($PSCmdlet)
  89.  } catch {
  90.      throw
  91.  }
  92. }
  93.  
  94. process
  95. {
  96.     try {
  97.         $steppablePipeline.Process($_)
  98.     } catch {
  99.         throw
  100.     }
  101. }
  102.  
  103. end
  104. {
  105.     try {
  106.         $steppablePipeline.End()
  107.     } catch {
  108.         throw
  109.     }
  110. }
  111. <#
  112.  
  113. .ForwardHelpTargetName Select-Object
  114. .ForwardHelpCategory Cmdlet
  115.  
  116. #>
  117. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me