Removal of multiple entries in test file

Hi Everyone,

Need a help on a logic for which powershell script need to built. I have some files in txt which has " C:\Windows\system32;C:\Windows;C:\Windows\System32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32" where C:\Windows\System32 is repeating 3 times which is not required. I need to replace the 3 entries as Single Entry. Can anyone help me out on the same.
I tried replace string but the logic is missing, If we want to replace A then the value of A will change to B or whatever. But here I need to replace the 3 entries as Single entry.

Thanks in advance if someone helpme out on this.

[attachment file=“Duplicate Values.txt”]

I didn’t spend much time with this, but the option below will work. Notice that it’s going to make everything lower case (-Unique will think C:\Windows\system32 and C:\Windows\System32 are different unless we modify the case). If that’s going to make an impact then someone (perhaps even me) will have to come up with a better idea.

((Get-Content C:\Duplicate-Values.txt) -split ';').ToLower() | Select-Object -Unique

And, just in case you want to see this done (very) procedurally…

$x = Get-Content C:\Duplicate-Values.txt
$x = $x.Split(';')
$x = $x.ToLower()
$x = $x | Select-Object -Unique
$x | Set-Content C:\Duplicate-Values.txt

In the second example, we’re writing over the original contents of Duplicate-Values.txt. You can do this in the first example by piping your Select-Object output to Set-Content.

Hi Tom,

Thanks for your time. I think what I asked and what you suggest both are absolutely different. I just want to make 3 same entries as single entry.

Hi Manoj,

I think tomy had a good solution.

Please try out this example wich is just a variant of what tommy wrote:

$String = "C:\Windows\system32;C:\Windows;C:\Windows\System32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32"
$PathList = $String.ToLower() -split ';'
# $PathList is now a list of all entries
$PathList
# Select unique entries form list (remove all duplicates):
$UniquePaths = $PathList | Select-Object -Unique
# Combine list to a string again
$NewString = $UniquePaths -join ';'
$NewString

Hey,

Here is an alternative version which does not lowercase the path items.

$Path = 'C:\Windows\system32;C:\Windows;C:\Windows\System32;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32'

# Array which dynamically increases as required
$Array = New-Object -TypeName System.Collections.ArrayList

$Path -split ';' |  ForEach-Object { 
    if ($Array -notcontains $_) { 
        [void]($Array.Add($_))
    }
}

$NewPath = $Array -join ';'
$NewPath