The task is to create users in AD and add them to existing groups.

That`s the CSV file:002.PNG

That`s the script:

That`s the error:

How can this be resolved? (Not sure the script is even correct, but for now can`t execute so no way to check)

2 Spice ups

The script and the error seems to be missing?

If you post code, please use the ‘Insert Code’ button. Please and thank you!

2 Spice ups

Maybe something like this:

$DCName = "dcserver01"

foreach($aduser in (import-csv "c:\file.csv")){
    $props = @{
        SamAccountName = $aduser.SamAccountName
        name           = $aduser.name
        path           = $aduser.parentou
        server         = $DCName
    }
    New-ADUser @props

    Add-ADGroupMember -Identity $aduser.group -Members $aduser.SamAccountName -Server $DCName
}

You want to give it the same servername (that assumes you have multiple DC) so you have no issues with replication.
It also assumes the Groups names you have in your CSV is that actual name of a group.

Yes, it disappeared. That`s the code:

Import-Module ActiveDirectory
Import-Csv "C:\Scripts\orgusers.csv" | ForEach-Object {
 $userPrincinpal = $_."samAccountName" + "@lovely.Local"
New-ADUser -Name $_.Name `
 -Path $_."ParentOU" `
 -SamAccountName  $_."samAccountName" `
 -UserPrincipalName  $userPrincinpal `
 -AccountPassword (ConvertTo-SecureString "Pa$$w0rd" -AsPlainText -Force) `
 -Enabled $true
Foreach-Object {add-adgroupmember -Identity $_.group -Members $_.name}

And that`s the error:

Well that makes no sense.

Either you call it by typing in the entire path

e.g. C:\scripts\orgusers.ps1

OR you go to the right directory and execute it from there

set-location c:\scripts
.\orgusers.ps1

The way you typed it in powershell thinks scripts is a cmdlet

Try it like so:

Import-Module ActiveDirectory

$dcname = 'dc01' #add you DC name here

Import-Csv "C:\Scripts\orgusers.csv" | 
ForEach-Object {
    $prop = @{
        name              = $_.name
        path              = $_.parentou
        SamAccountName    = $_.samaccountname
        UserPrincipalName = $_."samAccountName" + "@lovely.Local"
        AccountPassword   = (ConvertTo-SecureString "Pa$$w0rd" -AsPlainText -Force)
        Enabled           = $true
        Server            = $dcname
    
    }
    New-ADUser @prop
    
    Add-ADGroupMember -Identity $_.group -Members $_.name -Server $dcname
}

Thank you. Changed the code, now it gives the error for each user creation attempt. Does it mean something is wrong with the .csv file itself?

possibly.

This worked for me just fine, see the attached screenshot

I’d try to re-create the CSV, make sure it is a CSV and not an exel file.

Make sure those OU actually exist and that there are no whitespaces.

Thank you, I will check everything and try again.