Tuesday, March 22, 2016

Using Get-Unique to filter duplicated result

We can use Get-Unique to filter duplicate data from a result from a list in PowerShell. This cmdlet is available on PowerShell 2.0 or later.

How can we use it?
For example, there is a file with duplicate data.


We can perform "Get-Content <a file name> | Get-Unique


The duplicate date was filtered on the PowerShell result.

One more example, I'd like to filter out AD User from AD groups.

"Get-ADGroupMember <AD Group name> | where objectclass -eq group | Select Name"


Get AD user from these groups by performing "Get-ADGroupMember <The root AD Group name> | where objectclass -eq group | Get-ADGroupMember | where objectclass -eq User | Select name,SID


There are 2 AD users are the member of Group1 and Group2. To unique the result, perform "Get-ADGroupMember <The root AD Group name> | where objectclass -eq group | Get-ADGroupMember | where objectclass -eq User | Sort name | Get-Unique | Select Name,SID"


The duplicate AD users were removed from the result.

This posting is provided “AS IS” with no warranties, and confers no rights!

No comments:

Post a Comment