Have a bunch of values from csv file, with column looking like this:
shouston
cgonzalez
bbrown
hlader
kpesavento
jbloom
polson
bcharlow
bcharlow
bkalt
Need to find duplicates and modify them.
# Grab CSV file
$inputFile = Import-Csv -Path $filePath
$text = (Get-Culture).TextInfo
$HashSet = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
foreach ($line in $inputFile) {
$name = $line.name
$line.name = $text.ToTitleCase($name)
$firstName = $line.name.split(" ")[0]
$lastName = $line.name.split(" ")[1]
$newEmail = ($firstName[0] + $lastName).toLower()
if (!$HashSet.Add($newEmail)) {
$line.email = ($firstName[0] + $lastName + $line.location_id + "@abc.com").toLower()
}
else {
$line.email = ($firstName[0] + $lastName + "@abc.com").toLower()
}
}
$inputFile | Export-Csv $fullPath
But its not working as I expected - find a duplicate values and modify them. In my case, the location must also be added to the first duplicate email, example bcharlow1 bcharlow2, not it's bcharlow, bcharlow2. Can anyone help me - how I need to modify script?
You can use
Group-Object
to see the duplicates, so doing something like :Will give you a count of how many times the given value is been seen. If you want to see lets say the top 3 you can pipe it to sort and select