First of all I would like to thank you for helping me or at least reading my post
I have an CSV file name as Users with two values
firstname,lastname
John,Doe
Jane,Doa
I would like to use these values and create many values with it and afterwards export it to another CSV
This is the code
$users = Import-Csv C:\Temp\users.csv
foreach ($user in $users) {
$firstname = $user.firstname
$lastname = $user.lastname
$username = ($firstname.ToLower()+"."+$lastname.ToLower()) -replace '\s',''
$email = $username+"@email.com"
$Location = "Paris"
$ou = "OU=Users,OU=ORG,DC=test,DC=local"
Select-Object $firstname,$lastname,$username,$email,$Location,$ou | export-csv C:\Temp\userstest.csv
}
The issue with this code is: I get an empty CSV as export
Many Thanks
You're re-exporting the csv with every single row, this is causing your issue.
Instead, let's shift the order of commands and make this work. Furthermore, you could use Calculated Properties to make this even shorter, like this.
$users = Import-Csv C:\Temp\users.csv
$Users | Select firstname, lastname,`
@{l='username';exp={($_.firstname.ToLower()+"."+$_.lastname.ToLower()) -replace '\s',''}},`
@{l='email';exp={"$($($_.firstname.ToLower()+"."+$_.lastname.ToLower()) -replace '\s','')@email.com"}},@{l="Location";exp={"Paris"}}, `
{l='ou';exp={"OU=Users,OU=ORG,DC=test,DC=local"}} | export-csv c:\temp\some.csv
Tested it on my PC and it worked just fine!