Link to home
Start Free TrialLog in
Avatar of Loyall
LoyallFlag for Netherlands

asked on

Powershell Create unqiue AD users by adding ascending number to samaccountname

Hi,

The company I'm working  at has a username policy: 2 first characters Givenname + 2 first characters Surname + ##
Example: John Smith will be given username josm01

I have this script that creates users. A part of the script defines samaccountname:

$sam = $_.Givenname.substring(0,2).ToLower() + $_.Lastname.substring(0,2).ToLower() + "01"
        Try   { $exists = Get-ADUser -LDAPFilter "(sAMAccountName=$sam)" }
        Catch { }
        If(!$exists)

Open in new window


If the username does not exist, the user is created.
If the username DOES exist, a line is written to a logfile and the creation of that user is cancelled.

I would like to have an adaption to the script:
If the username is found, the 01 is incremented by 1, to 02, or to the first available number.

Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Loyall

ASKER

@oBdA

Thank you !
Works great !