Home > Software engineering >  If AD account exists, append a counter to the username started at 2
If AD account exists, append a counter to the username started at 2

Time:11-04

The following script is adding accounts to the Active Directory. In case the username already exists, I want to append a number to the username and try again.

i.e. if cs15csa already exists, it should try again with cs1csa2. If cs1csa2 exists, it should then try with cs1csa3 and so on and so forth.

How do I do that?

# Enter a path to your import CSV file
$ADUsers = Import-csv export.csv

foreach ($User in $ADUsers)
{
       $Username    = $User.username
       $Password    = $User.password
       $Firstname   = $User.firstname
       $Lastname    = $User.lastname
       $OU          = $User.ou

       # Check if the user account already exists in AD
       if (Get-ADUser -F {SamAccountName -eq $Username})
       {
              # If user does exist, output a warning message
              Write-Warning "A user account $Username ($Firstname $Lastname) already exists in the Active Directory."
       }
       else
       {
              # If a user does not exist then create a new user account
              # Account will be created in the OU listed in the $OU variable in the CSV file; don't forget to change the domain name in the"-UserPrincipalName" variable
              New-ADUser `
              -SamAccountName $Username `
              -UserPrincipalName "[email protected]" `
              -Email "[email protected]" `
              -ProfilePath '\\leo\%USERNAME%\Profile' `
              -Name "$Username" `
              -GivenName $Firstname `
              -Surname $Lastname `
              -Enabled $True `
              -DisplayName "$Firstname $Lastname" `
              -Path $OU `
              -AccountPassword (convertto-securestring $Password -AsPlainText -Force)
       }
}

CodePudding user response:

You can simply use a loop to test the SamAccountName and inside keep adding a counter number to it until you have found a unique name.

To avoid having to use those nasty backticks on the New-ADUser cmdlet, I would advise to use Splatting

Also, '\\leo\%USERNAME%\Profile' should be "\\leo\$Username\Profile"

Try

# Enter a path to your import CSV file
$ADUsers = Import-Csv export.csv

foreach ($User in $ADUsers) {
    $Username    = $User.username

    # Check if the user account already exists in AD and keep adding 
    # a counter value to the SamAccountName until unique
    $count = 2
    while (Get-ADUser -Filter "SamAccountName -eq '$Username'") {
        $Username = '{0}{1}' -f $User.username, $count  
    }
    # create the new user using a Splatting Hashtable
    $userParams = @{
        SamAccountName    = $Username
        UserPrincipalName = "[email protected]"
        EmailAddress      = "[email protected]"
        ProfilePath       = "\\leo\$Username\Profile"
        Name              = $Username
        GivenName         = $User.firstname
        Surname           = $User.lastname
        Enabled           = $true
        DisplayName       = '{0} {1}' -f $User.firstname, $User.lastname
        Path              = $User.ou
        AccountPassword   = $User.password | ConvertTo-SecureString -AsPlainText -Force
    }
    # create the user
    New-ADUser @userParams
}
  • Related