locked
import users from excel file error RRS feed

  • Question

  • hello i'm having trouble with importing bulk users from excel file.

    i got the script from here: https://activedirectorypro.com/create-bulk-users-active-directory/

    script:

    # Import active directory module for running AD cmdlets
    Import-Module activedirectory
      
    #Store the data from ADUsers.csv in the $ADUsers variable
    $ADUsers = Import-csv C:\Tools\bulk_users1.csv
    
    #Loop through each row containing user details in the CSV file 
    foreach ($User in $ADUsers)
    {
    	#Read user data from each field in each row and assign the data to a variable as below
    		
    	$Username 	= $User.username
    	$Password 	= $User.password
    	$Firstname 	= $User.firstname
    	$Lastname 	= $User.lastname
    	$OU 		= $User.ou #This field refers to the OU the user account is to be created in
        $Description = $User.description
        $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 to see if the user already exists in AD
    	if (Get-ADUser -filter {SamAccountName -eq $Username})
    	{
    		 #If user does exist, give a warning
    		 Write-Warning "A user account with username $Username already exist in 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@cloudhub.global" `
                -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 $True
                
    	}
    }
    

    when i run i always get the following error :

    Get-ADUser : Variable: 'Username' found in expression: $Username is not defined.
    At C:\Tools\bulk_users1.ps1:32 char:6
    +     if (Get-ADUser -filter {SamAccountName -eq $Username})
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Com
       mands.GetADUser

    i don't know what is wrong ....can someone please help me ??

    thanx


    • Edited by evidesmedt Friday, July 20, 2018 6:51 AM
    Friday, July 20, 2018 6:49 AM

Answers

  • $ADUsers = Import-csv C:\Tools\bulk_users1.csv -delimiter ';'



    \_(ツ)_/

    • Marked as answer by evidesmedt Friday, July 20, 2018 10:28 AM
    Friday, July 20, 2018 9:48 AM

All replies

  • Get-ADUser -filter "SamAccountName -eq '$Username'"

    \_(ツ)_/

    Friday, July 20, 2018 6:59 AM
  • hi thanx for the respone but unfortunate this doesn't fix it :i get this:

    Get-ADUser : The search filter cannot be recognized
    At C:\Tools\bulk_users1.ps1:32 char:6
    +     if (Get-ADUser -filter "SamAccountName -eq '$Username'")
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-ADUser], ADException
        + FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    Friday, July 20, 2018 8:52 AM
  • If the username is null that will happen.


    \_(ツ)_/

    Friday, July 20, 2018 8:54 AM
  • isn't it getting the username from the excel file ??? :/

    there is a column usernameexcel: Firstname;middleInitial;Lastname;Username;Description;email;streetaddress;city;zipcode;state;country;department;Password;telephone;jobtitle;Company;OU

    Friday, July 20, 2018 9:45 AM
  • $ADUsers = Import-csv C:\Tools\bulk_users1.csv -delimiter ';'



    \_(ツ)_/

    • Marked as answer by evidesmedt Friday, July 20, 2018 10:28 AM
    Friday, July 20, 2018 9:48 AM
  • Awesome ! it worked :)

    thank you very much for taking the time and effort with this ! much appreciated

    Friday, July 20, 2018 10:28 AM