Import the users provided in the excel file in their appropriate OU.

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 2 months, 2 weeks ago.

  • Author
  • #70448
    Profile photo of zacforn

    Just need a script that add users in appropriate ou .
    #Import the PowerShell module containing AD cmdlets
    Import-Module ActiveDirectory

    write-host "Start Process"
    write-host "————————————-"

    #Read the CSV file
    $csvPath = "C:\AccountsImportFile.csv"
    $csvData = import-csv $csvPath

    write-host "Reading the CSV file......"
    #Loop through all items in the CSV items

    ForEach ($user In $csvData)
    $saMAccountName = $user.sAMAccountName

    #Check if the User exists
    $ADuser = Get-ADUser -LDAPFilter "(sAMAccountName=$saMAccountName)"

    If ($ADuser -eq $Null)
    #Create user using New-ADUser cmdlet
    $userPrincipalName = $user.sAMAccountName + ""

    New-ADUser -Name $user.displayName`
    -SamAccountName $sAMAccountName `
    -UserPrincipalName $userPrincipalName `
    -GivenName $user.givenname `
    -Surname $ `
    -DisplayName $user.displayName `
    -AccountPassword (ConvertTo-SecureString "Pa`$`$w0rd" -AsPlainText -Force) `
    -PasswordNeverExpires $true `
    -ChangePasswordAtLogon $false `
    -Enabled $true

    write-host "- " $user.sAMAccountName "| Account Created" -ForegroundColor green

    write-host "- " $user.sAMAccountName "|Account Exists" -ForegroundColor yellow

    write-host "Error: " $($_.CategoryInfo) -ForegroundColor red
    write-host "Message: " $($_.Exception.Message) -ForegroundColor red


    write-host "—————————————————————–"
    write-host "End Process"

  • #70456
    Profile photo of Rob Simmers
    Rob Simmers

    New-ADUser has a -Path attribute to specify the OU path. You may want to also take a look at splatting.

You must be logged in to reply to this topic.