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.
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.
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.
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