PoshCode Archive  Artifact Content

Artifact 84110b816067df22c67da47df63075ecb2d792b56aac33bd7be14ea5b304c41a:

  • File where-property.ps1 — part of check-in [8f29baa863] at 2018-06-10 13:12:28 on branch trunk — different examples of how you can access properties with a custom where function (user: karl prosser size: 2174)

# encoding: ascii
# api: powershell
# title: where-property
# description: different examples of how you can access properties with a custom where function
# version: 0.1
# type: function
# author: karl prosser
# license: CC0
# function: where-property
# x-poshcode-id: 2570
# x-archived: 2012-12-29T04:10:48
# x-published: 2012-03-18T12:27:00
#
#


function where-property([string] $PropertyName,[string]$SubProperty , $is,$isnot,$contains,$in)  
{
 process {
    if ($is) 
        {
           if ($_.$Propertyname -eq $is) { , $_ }
        }
    elseif ($isnot) 
        {  
            if ($_.$Propertyname -ne $is) { , $_ } 
        }
    elseif($contains)
        { 
                if ($subproperty)
                {
                    foreach ($prop in  $_.$propertyname )
                    {
                      if ($prop)
                       {
                         $subpropertyvalue = $prop.$subproperty
                         if ($subpropertyvalue -contains $contains ) { , $_ } 
                       }
                    }
                }
                else 
                {  
                    if ($_.$Propertyname -contains $contains) { , $_ }            
                }
            
        }
    elseif($in)
        { 
            if ($in -contains $_.$Propertyname) { , $_ }
        }    
     
    
 }
}
#set-alias and-property where-property


#get-processes with a specific name
gps | where-property processname -is svchost
#get-processes all but a specific name
gps | where-property processname -isnot svchost
#get-processes with the processname in a specific list
gps | where-property processname -in iexplore,chrome
#get verbs in a specific group
get-verb | where-property group -in common


#get-commands that have a specific named parameter
 get-command | where-property parameters -subproperty keys  -contains Begin 
#and using an a lias to this for the and since it seems more HUMAN to say where X and Y, rather than Where x where y 
 get-command | where-property parameters -subproperty keys  -contains Name |  and-property commandtype -is alias