I am trying to change the domains of emails inside a text file for example "[email protected] to [email protected]". The emails are stored in a array and I am currently using a for loop with the replace method but I cannot get it to work. Here is the code that I have so far.
$folders = @('Folder1','Folder2','Folder3','Folder4','Folder5')
$names = @('John','Mary','Luis','Gary', 'Gil')
$emails = @("[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]")
$emails2 = @("[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]")
$content = "C:\Users\john\Desktop\contact.txt"
#create 10 new local users
foreach ($user in $users){ New-LocalUser -Name $user -Description "This is a test account." -NoPassword }
#Create 5 folders on desktop
$folders.foreach({New-Item -Path "C:\Users\John\Desktop\$_" -ItemType directory})
#create 5 folders on in documents
$folders.foreach({New-Item -Path "C:\users\john\Documents\$_" -ItemType directory})
#create contact.tct
New-Item -Path "C:\Users\John\Desktop" -Name "contact.txt"
#add 5 names to file
ForEach($name in $names){$name | Add-Content -Path "C:\Users\John\Desktop\contact.txt"}
#add 5 emails to file
ForEach($email in $emails){$email | Add-Content -Path "C:\Users\John\Desktop\contact.txt"}
#change emails to @comapny.com
for($i = 0; $i -lt 5; $i++){$emails -replace "$emails[$i]", $emails2[$i]}
"$emails[$i]"because that causes string expansion. Everything after the variable name starting with[will just be appended to the string verbatim. Just use$emails[$i]instead. Since-replacedoesn't actually update the stored array, you will need to update$emailson every iteration to keep your changes. Then output$emailsoutside of the loop at the end.