Get Unique - PowerShell


Overview

Get-Unique is a versatile PowerShell command designed for efficiently identifying and extracting unique values from a specified input dataset. It excels in scenarios where duplicate values need to be eliminated, allowing users to work with distinct data points for further analysis or processing.

Syntax

Get-Unique [-InputObject] <Object> [-ErrorAction] <ErrorAction> [-ErrorVariable] <Variable> [-PassThru]

Parameters:

  • -InputObject: Specifies an input object or collection of objects to be processed for unique values.
  • -ErrorAction: Defines the action to be taken when errors occur. Default: Stop.
  • -ErrorVariable: Assigns an error to a specified variable for further handling.
  • -PassThru: Passes through the input objects unchanged, alongside the unique values.

Options/Flags:

None

Examples

Simple Usage: Extract Unique Values from an Array

$array = 1, 2, 3, 3, 4, 5, 5, 6
Get-Unique -InputObject $array

Complex Usage: Filter Unique Objects from a Collection

$objects = Get-ChildItem -Directory
Get-Unique -InputObject $objects -Property LastWriteTime

Common Issues

Issue: Command returns no results.
Solution: Ensure the input data contains at least one unique value.

Integration

Get-Unique can be integrated with other commands using the pipeline:

Combine with Select-Object to Extract Specific Properties

Get-ChildItem -Directory | Select-Object -Property LastWriteTime | Get-Unique

Use with Group-Object to Find Unique Groupings

Get-ChildItem -Directory | Group-Object -Property LastWriteTime -NoElement | Get-Unique
  • Get-Duplicates: Identifies and extracts duplicate values from an input dataset.
  • Sort-Object: Sorts input objects based on specified criteria, which can be useful for grouping unique values.