Add user in AD from csv file

rom1 21 Reputation points
2021-04-02T08:50:12.23+00:00

Hello, I 'm trying to add users from csv file but I have an error with my script could someone help me?

Scrip file:
Import-Module activedirectory

Stocke les informations du fichier add_users dans les variables $ADUsers

$ADUsers = Import-csv C:\TEMP\add_users.csv

Parcoure chaque ligne contenant les détails de l'utilisateur dans le fichier CSV

foreach ($User in $ADUsers)
{

Lit les données utilisateur de chaque champ de chaque ligne et affectez les données à une variable comme ci-dessous:

$Username   = $User.username  
$Password   = $User.password  
$Firstname  = $User.firstname  
$Lastname   = $User.lastname  
$OU         = $User.ou #Ce champ fait référence à l'OU dans laquelle le compte utilisateur doit être créé  
$email      = $User.email  
$streetaddress = $User.streetaddress  
$city       = $User.city  
$zipcode    = $User.zipcode  
$state      = $User.state  
$country    = $User.country  
$telephone  = $User.telephone  
$jobtitle   = $User.jobtitle  
$company    = $User.company  
$department = $User.department  
$Password = $User.Password  

Check si l'utilisateur existe déjà dans l'AD

if (Get-ADUser -F {SamAccountName -eq $Username})  
{  

Si l'utilisateur existe, affiche un avertissement:

     Write-Warning "Un compte d'utilisateur avec le nom d'utilisateur $ Username existe déjà dans l'Active Directory."  
}  
else  
{  

User does not exist then proceed to create the new user account

    #Account will be created in the OU provided by the $OU variable read from the CSV file  
    New-ADUser `  
        -SamAccountName $Username `  
        -UserPrincipalName "$Username@PaperCut.local" `  
        -Name "$Firstname $Lastname" `  
        -GivenName $Firstname `  
        -Surname $Lastname `  
        -Enabled $True `  
        -DisplayName "$Lastname, $Firstname" `  
        -Path $OU `  
        #-City $city `  
        #-Company $company `  
        #-State $state `  
        #-StreetAddress $streetaddress `  
        #-OfficePhone $telephone `  
        -EmailAddress $email `  
        #-Title $jobtitle `  
        #-Department $department `  
        -AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $False  

     #Si l'utilisateur n'existe pas, affiche le message:  
     Write-Warning "Un compte d'utilisateur $Username a été crée dans l' Active Directory."  
}  

}
Read-Host -Prompt "Press Enter to exit"

my csv file
83917-image.png

message error:
![83946-image.png]2

Thx for reading me

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,822 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
{count} votes

Accepted answer
  1. Andreas Baumgarten 95,496 Reputation points MVP
    2021-04-02T09:52:32.14+00:00

    Hi @rom1 ,

    could you please try this:

    # Replace this:  
    if (Get-ADUser -F {SamAccountName -eq $Username})  
      
    # With this:  
    if (Get-ADUser -Filter * | Where-Object {SamAccountName -eq $_.Username})  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,486 Reputation points Microsoft Vendor
    2021-04-02T09:32:23.663+00:00

    Hi,

    Please make sure the path of the imported CSV file is correct. What is the value of $ADUsers or $ADUsers.username?

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments