The Get-Unique cmdlet is a powerful tool in Windows PowerShell that enables users to quickly and easily extract unique values from a set of data. It can be used to filter out duplicate values from a list, or to compare two sets of data and extract only the unique values from each set. In this article, we’ll explore the syntax, parameters, inputs, outputs, examples, and tips for using the Get-Unique cmdlet.

Syntax

The syntax for the Get-Unique cmdlet is as follows:

Get-Unique [-InputObject] <Object[]> [-Property <String[]>] [-Unique] [-CaseSensitive] [-AsString] [-Exclude <Object[]>] [-Include <Object[]>] [-InputObject <PSObject>] [-LiteralPath <String[]>] [-Path <String[]>] [-Culture <String>] [-Encoding <String>] [-ExcludeObject <PSObject>] [-Filter <String>] [-IncludeObject <PSObject>] [-TotalCount <Int32>] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

Parameters

The Get-Unique cmdlet has several parameters that can be used to customize its output. These parameters are listed in the table below:

Parameter Description
-InputObject The objects to be compared or filtered.
-Property The property of the objects to be compared or filtered.
-Unique Specifies that only unique values should be returned.
-CaseSensitive Specifies that the comparison should be case-sensitive.
-AsString Specifies that the output should be returned as a string.
-Exclude Specifies the objects to be excluded from the comparison or filtering.
-Include Specifies the objects to be included in the comparison or filtering.
-InputObject Specifies the objects to be compared or filtered.
-LiteralPath Specifies the path to the file or folder to be compared or filtered.
-Path Specifies the path to the file or folder to be compared or filtered.
-Culture Specifies the culture to be used for comparison or filtering.
-Encoding Specifies the encoding to be used for comparison or filtering.
-ExcludeObject Specifies the objects to be excluded from the comparison or filtering.
-Filter Specifies the filter to be used for comparison or filtering.
-IncludeObject Specifies the objects to be included in the comparison or filtering.
-TotalCount Specifies the total number of objects to be compared or filtered.
-Force Specifies that the cmdlet should continue even if errors occur.
-WhatIf Specifies that the cmdlet should simulate its actions without actually performing them.
-Confirm Specifies that the cmdlet should prompt the user for confirmation before performing its actions.

Inputs

The Get-Unique cmdlet accepts the following inputs:

  • Objects
  • Property
  • Exclude
  • Include
  • InputObject
  • LiteralPath
  • Path
  • Culture
  • Encoding
  • ExcludeObject
  • Filter
  • IncludeObject
  • TotalCount

Outputs

The Get-Unique cmdlet produces the following outputs:

  • Objects
  • Strings

Examples

The following examples demonstrate how to use the Get-Unique cmdlet:

  • To filter out duplicate values from a list of objects, use the following command:

    Get-Unique -InputObject <Object[]> [-Property <String[]>] [-Unique] [-CaseSensitive] [-AsString] [-Exclude <Object[]>] [-Include <Object[]>] [-InputObject <PSObject>] [-LiteralPath <String[]>] [-Path <String[]>] [-Culture <String>] [-Encoding <String>] [-ExcludeObject <PSObject>] [-Filter <String>] [-IncludeObject <PSObject>] [-TotalCount <Int32>] [-Force] [-WhatIf] [-Confirm] [<CommonParameters>]

  • To compare two sets of data and extract only the unique values from each set, use the following command:

    Get-Unique -InputObject <Object[]> [-Property <String[]>] [-Unique] [-CaseSensitive] [-AsString] [-Exclude <Object[]>] [-Include <Object[]>] [-InputObject <PSObject>] [-LiteralPath <String[]>] [-Path <String[]>] [-Culture <String>] [-Encoding <String>] [-ExcludeObject <PSObject>] [-Filter <String>] [-IncludeObject <PSObject>] [-TotalCount <Int32>] [-Force] [-WhatIf] [-Confirm]

FAQ

How do I get-unique values in PowerShell?

To obtain unique values in PowerShell, use the Get-Unique cmdlet. This command compares a sorted list’s items, removing duplicates and displaying only one instance of each item. For accurate results, ensure the list is sorted, as the cmdlet is case-sensitive and considers strings that only differ in casing to be unique. Optimize your PowerShell usage by utilizing the Get-Unique cmdlet.

How do I get-unique values from an array in PowerShell?

To get unique values from an array in PowerShell, you can use the combination of Get-Unique and Sort-Object. Simply create an array with duplicate values, sort it using Sort-Object, and then apply Get-Unique to remove any duplicate values from the array. This will give you the unique values in the PowerShell array.

How do I remove duplicates in PowerShell?

To remove duplicates in PowerShell, there are various methods you can use. You can employ cmdlets such as Select-Object, Sort-Object, Get-Unique, or Group-Object. These cmdlets allow you to easily eliminate duplicate values from an array. So, removing duplicates in PowerShell is a common task that can be accomplished seamlessly with the right cmdlet.

How to use get-command in PowerShell?

To use Get-Command in PowerShell, you can leverage the wildcard character (*) to narrow down your search. For example, type “Get-Command Start-” to get a list of cmdlets starting with “Start,” or “Get-Command *-Process” for cmdlets containing “Process”. This helps you find the right cmdlets when starting a process.

Leave a Reply