Giter VIP home page Giter VIP logo

select-unique's Introduction

Select-Unique

Description

PowerShell function that helps getting unique objects by one or multiple properties. Similar to Sort-Object -Unique but without any sorting and a bit faster.

Parameters

Parameter Description
InputObject The function is intended to work taking input from pipeline, this parameter bounds from pipeline and should not be used manually.
On Property names used to filter for unique values on the objects. This parameter allows wildcards. See PSMemberInfoCollection<T>.Match Method.
CaseSensitive Specifies if the filtering should be case sensitive when this switch is activated.
Select Similar usage as Select-Object. Allows the use of calculated properties.

Examples

  • Get unique processes on ProcessName property:
Get-Process | Select-Unique -On ProcessName
  • Get unique processes on ProcessName and Id properties:
Get-Process | Select-Unique -On ProcessName, Id
  • Get unique processes on any property starting with Process and Id property:
Get-Process | Select-Unique -On Process*, Id
  • Get unique processes on ProcessName property and select property ProcessName as Name:

Note: for calculated properties, the hashtable Keys will be the new property name and the Value can be a string representing the original object property name or a expression (ScriptBlock). The example after this one demonstrates the use of a ScriptBlock in a calculated property.

Get-Process | Select-Unique -On ProcessName -Select @{ Name = 'ProcessName' }
  • Get unique processes on Id property and select property ProcessName as Name and property WorkingSet64 using a calculated expression to convert the values from bytes to megabytes:
Get-Process | Select-Unique -On Id -Select @(
    @{ Name = 'ProcessName' }
    @{ MemoryUsage = { [string]::Format('{0:N0} Mb', ($_.WorkingSet64 / 1Mb)) }}
)
  • -CaseSensitive filtering:
$test = @(
    [pscustomobject]@{ foo = 'Hello'; bar = 'World' }
    [pscustomobject]@{ foo = 'Hello'; bar = 'World' }
    [pscustomobject]@{ foo = 'Hello'; bar = 'WORLD' }
    [pscustomobject]@{ foo = 'Hello'; bar = 'WORLD' }
    [pscustomobject]@{ foo = 'HELLO'; bar = 'WORLD' }
    [pscustomobject]@{ foo = 'HELLO'; bar = 'WORLD' }
)

$test | Select-Unique -On * -CaseSensitive

foo   bar
---   ---
Hello World
Hello WORLD
HELLO WORLD

$test | Select-Unique -On *

foo   bar
---   ---
Hello World

Performance Measurements

Below is a performance comparison between Sort-Object -Unique and this function on 3 properties. Source code for the performance tests in perftests.ps1.

Average Results

Test                Average RelativeSpeed
----                ------- -------------
Select-Unique       2474.40 1x
Sort-Object -Unique 3046.13 1.23x

Results per Test Run

TestRun Test                TotalMilliseconds
------- ----                -----------------
      2 Select-Unique                 2327.17
      7 Select-Unique                 2344.63
      6 Select-Unique                 2346.56
      5 Select-Unique                 2371.46
      8 Select-Unique                 2437.54
     10 Select-Unique                 2552.35
      4 Select-Unique                 2575.18
      1 Select-Unique                 2578.40
      3 Select-Unique                 2588.34
      9 Select-Unique                 2622.40
      8 Sort-Object -Unique           2694.80
      5 Sort-Object -Unique           2774.94
      3 Sort-Object -Unique           2783.88
      2 Sort-Object -Unique           2807.91
      7 Sort-Object -Unique           2825.35
      6 Sort-Object -Unique           2826.13
     10 Sort-Object -Unique           2905.93
      4 Sort-Object -Unique           3065.95
      9 Sort-Object -Unique           3259.83
      1 Sort-Object -Unique           4516.62

select-unique's People

Contributors

santisq avatar

Stargazers

HRXN avatar René avatar Matthew Gray avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.