powershellactive-directorypasswordscsv-import

Individual passwords for AD user | Import via CSV


I am trying to import users to AD via CSV / PowerShell. The import now works fine, the users and all attributes are correct, but it seems like the passwords are not imported correctly from my .csv.

Here is my .ps1 im using:

#Import required modules
Import-Module ActiveDirectory

#Prompt user for CSV file path
$filepath = Read-Host -Prompt "Enter Path of CSV"

$securePassword = ConvertTo-SecureString $pass -AsPlainText -Force

# Import the file into a variable
$users = Import-Csv $filepath -Delimiter ";"

#Loop through each row and gather information
ForEach ($user in $users) {

    #Gather the user's information
    $fname = $user.FirstName
    $lname = $user.LastName
    $uname = $user.Username
    $email = $user.Email
    $jtitle = $user.Title
    $OUpath = $user.'Organizational Unit'
    $pass = $user.Password
    $SAM = $user.SAM

    #Create new AD user for each user in CSV file
    New-ADUser -Name "$fname $lname" -GivenName $fname -Surname $lname -UserPrincipalName $uname -SamAccountName $SAM -Path $OUpath -AccountPassword $securePassword -PasswordNeverExpires $true -Enabled $true -EmailAddress $email

    # Echo for every user created
}

Does anybody have a clue what the reason could be here?

Thanks, Marius


Solution

  • You are converting $pass to secure string before it is imported from the csv. So the fixed one:

    #Import required modules
    Import-Module ActiveDirectory
    
    #Prompt user for CSV file path
    $filepath = Read-Host -Prompt "Enter Path of CSV"
    
    # Import the file into a variable
    $users = Import-Csv $filepath -Delimiter ";"
    
    #Loop through each row and gather information
    ForEach ($user in $users) {
    
        #Gather the user's information
        $fname = $user.FirstName
        $lname = $user.LastName
        $uname = $user.Username
        $email = $user.Email
        $jtitle = $user.Title
        $OUpath = $user.'Organizational Unit'
        $pass = $user.Password
        $SAM = $user.SAM
        $securePassword = ConvertTo-SecureString $pass -AsPlainText -Force
       # The Rest