PoshCode Archive  Artifact [f09fa19012]

Artifact f09fa1901231de5bcf3f9c0057912ca643ff868335f4f5997e4f00d2452f0d05:

  • File Compare-Property.ps1 — part of check-in [38aabab23e] at 2018-06-10 13:05:33 on branch trunk — From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes (user: Lee Holmes size: 1280)

# encoding: ascii
# api: powershell
# title: Compare-Property.ps1
# description: From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes
# version: 0.1
# type: script
# author: Lee Holmes
# license: CC0
# x-poshcode-id: 2132
# x-archived: 2016-05-17T12:49:30
# x-published: 2011-09-09T21:40:00
#
#
##############################################################################
##
## Compare-Property
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Compare the property you provide against the input supplied to the script.
This provides the functionality of simple Where-Object comparisons without
the syntax required for that cmdlet.

.EXAMPLE

Get-Process | Compare-Property Handles gt 1000

.EXAMPLE

PS >Set-Alias ?? Compare-Property
PS >dir | ?? PsIsContainer

#>

param(
    ## The property to compare
    $Property,

    ## The operator to use in the comparison
    $Operator = "eq",

    ## The value to compare with
    $MatchText = "$true"
)

Begin { $expression = "`$_.$property -$operator `"$matchText`"" }
Process { if(Invoke-Expression $expression) { $_ } }