Home > Enterprise >  Powershell script to add new users to Active Directory from a CSV file fails to work. The error sugg
Powershell script to add new users to Active Directory from a CSV file fails to work. The error sugg

Time:04-07

I have written a Powershell script to add new users to Active Directory from a CSV file. When I run the script I get a bad syntax error in the New-ADuser section of the code. I do not see how the syntax could be wrong. My code editor doesn't pick up anything either. I have attached a screenshot of both the error message and CSV file.

# Import Active Directory module
Import-Module ActiveDirectory
 
# Open file dialog
# Load Windows Forms
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
 
# Create and show open file dialog
$dialog = New-Object System.Windows.Forms.OpenFileDialog
$dialog.InitialDirectory = $StartDir
$dialog.Filter = "CSV (*.csv)| *.csv" 
$dialog.ShowDialog() | Out-Null
 
# Get file path
$CSVFile = $dialog.FileName
 
# Import file into variable
# Lets make sure the file path was valid
# If the file path is not valid, then exit the script
if ([System.IO.File]::Exists($CSVFile)) {
    Write-Host "Importing CSV..."
    $CSV = Import-Csv -LiteralPath "$CSVFile"
} else {
    Write-Host "File path specified was not valid"
    Exit
}
 
# Lets iterate over each line in the CSV file
foreach($user in $CSV) {
 
    # Password
    $SecurePassword = ConvertTo-SecureString "$($user.'First Name'[0])$($user.'Last Name')$($user.'Employee ID')!@#" -AsPlainText -Force

 
    # Create new user
    New-ADUser -Name $user.'Full Name' `
                -GivenName $user.'First Name' `
                -Surname $user.'Last Name' `
                -EmailAddress $user.'Email Address' `
                -DisplayName $user.'Full Name' `
                -Path "$($user.'Organizational Unit')" `
                -ChangePasswordAtLogon $true `
                -AccountPassword $SecurePassword `
                -Enabled $([System.Convert]::ToBoolean($user.Enabled))
 
    # Write to host that we created a new user
    Write-Host "Created $Username / $($user.'Email Address')"
 
    # If groups is not null... then iterate over groups (if any were specified) and add user to groups
    if ($User.'Add Groups (csv)' -ne "") {
        $User.'Add Groups (csv)'.Split(",") | ForEach {
            Add-ADGroupMember -Identity $_ -Members "$($user.'First Name').$($user.'Last Name')"
            WriteHost "Added $Username to $_ group" # Log to console
        }
    }
 
    # Write to host that we created the user
    Write-Host "Created user $Username with groups $($User.'Add Groups (csv)')"
}
 
Read-Host -Prompt "Script complete... Press enter to exit."

Error Screenshot

CSV File

CodePudding user response:

Look at this enter image description here

enter image description here

Did you mean

DC=co

Also, you really need to look into splatting and error checking. You're writing a success message to the host after a failed iteration.

splatting

try/catch error checking

  • Related