1

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

2
  • 1
    [1] PLEASE do not post images of code/errors/data. why? lookee ... Why should I not upload images of code/data/errors when asking a question? - Meta Stack Overflow — meta.stackoverflow.com/questions/285551/… ///// [2] also, look into using splatting instead of all those error-inducing backticks. ///// [3] also also, add the 1st 3 lines of your CSV to the Question so that folks can see if it looks as it should. Commented Apr 6, 2022 at 15:24
  • Without seeing what's inside your csv it's hard to pin-point the issue. Seems to be pointing to what you're passing the cmdlets. To include your .Split() on your groups. Commented Apr 6, 2022 at 16:21

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.