Maria.A
Maria.A

Reputation: 43

Check for duplicate - ADUsers in bulk

I am trying to make a PowerShell script to add users in bulk through a csv file. If the username already exists I want to add the number 1 to the username. How can I do this? I thought I could maybe make an if?

foreach ($User in $ADUsers) {   

    # Selvlagde variabler for opprettelse av brukere
    $Password  = Get-RandomCharacters -length 20 -characters 'ABCDEFGHKLMNOPRSTUVWXYZabcdefghiklmnoprstuvwxyz1234567890!._?/-'
    $Username  = $User.GivenName.substring(0,3) + $User.SurName.substring(0,3)
    $Username  = $Username.Replace('æ','ae')
    $Username  = $Username.Replace('ø','o')
    $Username  = $Username.Replace('å','aa')
    $Username  = $Username.ToLower()
    $Username  = $Username.Trim()
    $Email       = $Username + '@ONPremiumIT.com'
    $DisplayName = $User.GivenName + ' ' + $User.SurName

    if (condition) {
        
    }


    # Bruker splatting for å lagre info om brukere
    $userParams = @{
        Path                  = $User.Path
        SamAccountName        = $Username
        UserPrincipalName     = $Email
        Name                  = "$($User.GivenName) $($User.SurName)"
        GivenName             = $User.GivenName
        Surname               = $User.SurName
        Enabled               = $true
        ChangePasswordAtLogon = $false
        DisplayName           = $Displayname
        Department            = $Department
        AccountPassword       = (ConvertTo-SecureString $Password -AsPlainText -Force)
    }
    
    New-ADUser @userParams

Upvotes: 0

Views: 772

Answers (1)

Theo
Theo

Reputation: 61148

You can do that by testing if a user with that SamAccountName already exists. Something like this:

foreach ($User in $ADUsers) {   

    # Selvlagde variabler for opprettelse av brukere
    $Password  = Get-RandomCharacters -length 20 -characters 'ABCDEFGHKLMNOPRSTUVWXYZabcdefghiklmnoprstuvwxyz1234567890!._?/-'
    $Username  = $User.GivenName.substring(0,3) + $User.SurName.substring(0,3)
    $Username  = $Username.Replace('æ','ae')
    $Username  = $Username.Replace('ø','o')
    $Username  = $Username.Replace('å','aa')
    $Username  = $Username.ToLower()
    $Username  = $Username.Trim()

    # test if a user with that SamAccountName already exists, add an index number if needed
    $n = 1                        # start index at 1
    $newName = $Username          # copy to a new variable
    while ($true) {               # start an endless loop
        $usr = Get-ADUser -Filter "SamAccountName -eq '$newName'" -ErrorAction SilentlyContinue
        if (!$usr) { 
            $Username = $newName  # assign the $Username variable the unique value
            break                 # exit the loop if the $username is unique in the domain
        }  
        # construct a new username by adding the index to it
        $newName = '{0}{1}' -f $Username, $n++
    }

    $Email       = $Username + '@ONPremiumIT.com'
    $DisplayName = $User.GivenName + ' ' + $User.SurName

    # Bruker splatting for å lagre info om brukere
    $userParams = @{
        Path                  = $User.Path
        SamAccountName        = $Username
        UserPrincipalName     = $Email
        Name                  = "$($User.GivenName) $($User.SurName)"
        GivenName             = $User.GivenName
        Surname               = $User.SurName
        Enabled               = $true
        ChangePasswordAtLogon = $false
        DisplayName           = $Displayname
        Department            = $Department
        AccountPassword       = (ConvertTo-SecureString $Password -AsPlainText -Force)
    }

    New-ADUser @userParams
}

P.S. The test for the username must come before doing more things with the $Username variable, like using it for the $Email variable. Otherwise, you could have duplicates in that too..

Upvotes: 1

Related Questions