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.
If you become an expert once upon a time your user name will be missleading.
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:
You may explain a little more detailed what you’re actually want to achieve. There might be a better way.
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.
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.
Actually that was just a joke. You don’t have to change your name.
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: