Creating usernames in a format (oneliner)

Welcome Forums General PowerShell Q&A Creating usernames in a format (oneliner)

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

6 months, 4 weeks ago.

  • Author
  • #102820

    Points: 0
    Rank: Member

    Hi everyone,

    I want to build a single script that fetches a format that stands in a database. This format tells the script how to build an username. We have multiple customers, and every customer uses a different format for their usernames for example:

    – (first letter givenname).(last name without spaces) = f.lastnamewithoutspaces
    – (first name without spaces)(last name without spaces) = firstnamelastname

    you get the idea. Does anyone know if you can build this into a single regular expression? I looked into it but it blew my mind and find it too difficult to wrap my head around since most of my finds were to validate if something was a username.

    Hope you guys can help me out!

    Kind regards,

  • #102821

    Points: 0
    Rank: Member

    I don't think you're going to be able to do this with a single regular expression. If I was doing this I'd get the format code and then use a switch statement with one case per format to then build the username. There are probably a limited number of formats you'd be using so make sure the codes are generic and can be used for multiple customers

    • #102826

      Points: 0
      Rank: Member

      Ok, do you mean that you would type the way it is supposed to be formatted in the database like:


      and in your script use the validators to see which format is used so you can build upon the given variables?

  • #102860

    Points: 428
    Helping Hand
    Rank: Contributor

    Building usernames is usually much more complex. If there is a John.Smith and you pass another John Smith, you have to increment, add a middle initial, or something to make the username unique. Typically the process has to have a method to:

    • Generate the username
    • Validate the username is not in use
    • Increment username based on standards (add number, use middle initial, etc.)
    • Return the unique username to generate account

    We typically have to build a function to return a unique username based on client requirements. It's a process versus a rule\regex.

  • #102898

    Points: 0
    Rank: Member

    I'd just use a code to indicate the format to be used. The pseudo-code would be


    switch (customerusercode){
    code1 {create user name}
    code2 {create user name}


    create user

The topic ‘Creating usernames in a format (oneliner)’ is closed to new replies.