Check if two values in array are the same and replace the second on by something else

Hey guys,

I’m newbie to PowerShell.

I can’t seem to figure out how to check if a value exists more than once in the array and how to change the repeated values to another form of value in the same array. I’m not sure if you understood me, but I’ll give you an example below.

$table = @(“Banana”,“Pear”,“Banana”,“Apple”,“Banana”,“Peach”)

So here, I would like the result to become like this (below):

$$table = @(“Banana”,“Pear”,“Banana1”,“Apple”,“Banana2”,“Peach”)

Could you please help me. That would be much appreciated.

Thank you very much in advance.

Beginner_Power590,
Welcome to the forum. :wave:t4:

If you become an expert once upon a time your user name will be missleading. :wink:

You can use

and its resulting Count property to determine if an element of an array exists more than once.

That’s actually impossible because arrays in PowerShell are immutable. Assumed it does not have to be the same array you can create another one with the desired output.

You check the count of an element an if it’s more than one you use a loop with a counter to produce the output you want:

$table = @('Banana','Pear','Banana','Apple','Banana','Peach')

$NewTable = 
    $table | 
        Group-Object | 
            ForEach-Object {
                if ($_.Count -eq 1) {
                    $_.Name
                }
                Else{
                    foreach ($Element in $_.Group) {
                        $Element + $ElementCount
                        $ElementCount ++
                    }
                }
            }
$NewTable

You may explain a little more detailed what you’re actually want to achieve. There might be a better way. :wink:

And BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Thank you for your help and your advice. I understand that my name can cause some misleading on what I am. And for what I actually want to achieve is realizing a code that can help me create unique email user for user who have the same name and last Name with some condition that goes with it.
If there are several people with the same name, the email of the second person with the same name will be the first two letters of the first name.lastname@gmail.ca. For the third it will be three first letters of the first name.lastname@gmail.ca. Thereafter, it will be three first of the first name.lastnameNN@gmail.ca where NN is a sequential number between 1 and 99.

The code that I have so far is this (Hopefully is that way you tell me to do).

function Add-Mail {
    Param (
        [String]$prenom,   [String]$nom)
   $prenom = get-sanitizedInput $prenom 
   $nom = get-sanitizedInput $nom 
   $email = $prenom + "." + $nom + "@momo.ca"
    return $email
}

#Creation de la collection
$data = @()

#Creation du fichier2
$output = foreach ($utilisateur  in Get-Content "Fichier.csv")            
{                 
	#Separation du nom par la premiere occurence d'un espace
    $prenom, $nom = $utilisateur -split " " , 2   
    $displayName = $prenom + ";" + $nom
	
	#Ecriture du nom separe par un point virgule dans le fichier
	write-output $displayName
	
	$email = Add-Mail $prenom $nom
	#Ajout des PSCustomObject dans la collection
	$UtilisateurHashtable = @{
		prenom      = $prenom
		nom      = $nom
		courriel      = $email
	}

	$data += [PsCustomObject]$UtilisateurHashtable
	#Affichage du courriel
	Write-Host $email            
} 

So hopefully again that you can help me for understanding of what I have to do.

Thank you in advance so much

Actually that was just a joke. You don’t have to change your name. :wink:

How many email addresses do you create at once? Most of the cases I know of only need to check one email at a time against an existing AD.

Your rules to create unique email addresses if there are addresses already taken are pretty complex. I’d recommend to reconsider those rules. We used to add increasing numbers to the first name when a new employee has the same first and last name like an existing one.

I’d use a loop increasing the numbers to add to the name to make it unique … something like this:

$FirstName = 'George'
$LastName = 'Harrison'

Remove-Variable -Name Count

$ExistingEmailAddresses =
'John.Lennon@momo.ca',
'Paul.McCartney@momo.ca',
'George.Harrison@momo.ca',
'Ringo.Starr@momo.ca',
'George1.Harrison@momo.ca',
'George2.Harrison@momo.ca'

do {
    $NewEmailAddress = '{0}{1}.{2}@momo.ca' -f $FirstName, $Count, $LastName
    $Count ++
} until (
    $ExistingEmailAddresses -notcontains $NewEmailAddress
)
$NewEmailAddress