jdhitsolutions / PSScriptTools

:wrench: :hammer: A set of PowerShell functions you might use to enhance your own functions and scripts or to facilitate working in the console. Most should work in both Windows PowerShell and PowerShell 7, even cross-platform. Any operating system limitations should be handled on a per command basis. The Samples folder contains demonstration script files
MIT License
875 stars 110 forks source link

Get-PSUnique enhancement #150

Open jdhitsolutions opened 1 month ago

jdhitsolutions commented 1 month ago

Discussed in https://github.com/jdhitsolutions/PSScriptTools/discussions/149

Originally posted by **scriptingstudio** June 8, 2024 Parameter `Property` makes the function more universal ```powershell param ( [Parameter(Position = 0, Mandatory, ValueFromPipeline)] [ValidateNotNullOrEmpty()] [object]$InputObject, [string[]]$Property ) ... # process block if ($Property) { foreach ($item in $InputObject) { $props = $item.PSObject.Properties.where{$_.name -in $Property} if (-not $props) {continue} if (-not $UniqueList.Exists({-not (Compare-Object $args[0].PSObject.properties.where{$_.name -in $Property}.value $props.value)})) { $UniqueList.add($item) } } } else { foreach ($item in $InputObject) { if (-not $UniqueList.Exists({-not (Compare-Object $args[0].PSObject.properties.value $item.PSObject.Properties.value)})) { $UniqueList.add($item) } } } ```
jdhitsolutions commented 1 month ago

Using a property might also improve how this function handles more complex objects.

jdhitsolutions commented 3 days ago

I have added this feature to v2.50.0 which has been published to the PowerShell Gallery.