Create New user file from imported CSV

Hi,

I’m really new to Powershell and trying to understand the syntax.

So i have a CSV-file, that contains new users in the format ‘First Name;LastName,Username,Password’, headers are mentioned in the CSV.

I’m trying to write a script, that will concatenate some of the fields and create a new CSV file, populated with those concatenations.
The goal is to create an email address for login and email.

This is what I have so far:

[/code]
Import-Module ActiveDirectory
$CSVImport = Import-CSV c:\output\C2303~00086.csv
ForEach ($NewUser in $CSVImport)
{
$samAccountName = $NewUser.Username
$FName = $NewUser.“First Name”
$LName = $NewUser.LastName
$Passwd = $NewUser.Password
$EmailDomain = “@domain.com
$Emailaddr = $Fname + “.” + $LName + $EmailDomain
$EmailLogin = $samAccountName + $EmailDomain

write-host $Emailaddr

}
[/code]

From what I understand, my concatenation is correct, however, the variable values are not taking, not sure why. What is wrong in this syntax?

Thanks in advance,
Hunicrem

hubert,
Welcome back to the forum. :wave:t4: Long time no see

Where do you know? :thinking:

Try this snippet:

$CSVImport = Import-CSV c:\output\C2303~00086.csv
$Result = 
ForEach ($NewUser in $CSVImport) {
    [PSCustomObject]@{
        sAMAccountName = $NewUser.Username
        FName          = $NewUser.'First Name'
        LName          = $NewUser.LastName
        Passwd         = $NewUser.Password
        Emailaddr      = '{0}.{1}@domain.com' -f $NewUser.'First Name', $NewUser.LastName
        UPN            = '{0}@domain.com' -f $NewUser.Username
    }
}

$Result

Thanks for your reply Olaf.

Unfortunately, I see the same thing, values stay empty:

Blockquote
sAMAccountName :
FName :
LName :
Passwd :
Emailaddr : .@domain.com
UPN : @domain.com

This is what the csv looks like:

Blockquote
|First Name|LastName|Username|Password|
|Jeff|doe|a99999|!IMFksd9!f|

So I still have no clue why the variables stay empty

greetz

If your CSV really uses pipe symbols as delimiter you have to change your Import-Csv command and add -Delimter '|'.

Thanks Olaf. That didn’t work. Our system uses ; as a delimiter, but this csv file is coming from somewhere else. I saved the file as a new CSV file, added delimiter ; and voila, variables are correctly being populated!

I think I can go on from here… Thanks a lot, and if I have another question, I know where to go :slight_smile:
greetz

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.