Share via


Removing Duplicate Values Using PowerShell

Requirement

To remove the duplicate values and process automation using PowerShell. We get inputs as text file which get updated every 30 minutes. It may contains some duplicate values. The text file will have server names. One after other.
SERVER1
SERVER2
SERVER3
SERVER2
We need to get Serial Number and send it to other text file as output.

Summary

There are multiple ways of doing it in PowerShell. Get-Unique was introduced in PowerShell 3.0 can be consumed here to achieve the task. Or use SORT-OBJECT

Help

help Get-Unique -Detailed

help Get-Unique -Examples

help Sort -Examples

Using Get-Unique

$server = 1,1,2,2,3,3,4,5

$server | Get-Unique

Using Sort Object

$server = 1,1,2,2,3,3,4,5

$server | Sort -Unique

Using Select Object

$server = 1,1,2,2,3,3,4,5

$server | Select -Unique

Solution

Get-Content C:\Temp\Server.txt | Sort-Object -Unique | %{
Get-WmiObject -Class Win32_BIOS -ComputerName $_ | Select SerialNumber 
}
Get-Content C:\Temp\Server.txt | Sort-Object -Unique | %{
Get-WmiObject -Class Win32_BIOS -ComputerName $_ | Select PSComputerName , SerialNumber 
} | Out-File C:\Temp\SerialNumber.txt

Video

View