question

rom1-0238 avatar image
0 Votes"
rom1-0238 asked AndreasBaumgarten commented

Add user in AD from csv file

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

windows-server-powershellwindows-active-directory
image.png (8.2 KiB)
image.png (56.2 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @rom1-0238 ,

Did the answer work for you? Are there any additional questions to this topic?

If you found the answer helpful, it would be great if you please mark it "Accept as answer". This will help others to find answers in Q&A

----------
Regards
Andreas Baumgarten

0 Votes 0 ·
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @rom1-0238 ,

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

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.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.