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

Welcome Forums General PowerShell Q&A Import the users provided in the excel file in their appropriate OU.

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
1 year, 9 months ago.

  • Author
    Posts
  • #70445

    Participant
    Points: 0
    Rank: Member

    #Import the PowerShell module containing AD cmdlets
    Import-Module ActiveDirectory

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

    try
    {
    #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 + "@intwkx.allaboutfood.com.mt"

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

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

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

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

    }

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

  • #70451

    Participant
    Points: 22
    Rank: Member

    Moving to appropriate forum.

  • #70466

    Participant
    Points: 527
    Helping Hand
    Rank: Major Contributor

    Adam, it looks like a duplicate post, I already answered this thread in the Powershell Q\A forum.

The topic ‘Import the users provided in the excel file in their appropriate OU.’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort