Strip name to use for AD

the challange i have is as follows. I want to split up the name of a person into usable AD usernames

the format of the firstname could be:

the fomat of the lastname could be:

examples:
Kees Jansen
Kees van der Hoog
Klaas-Jan de Bruin
WIllem van 't Veld

what should the end result be:
Kees Jansen - kjansen
Kees van der Hoog - kvdhoog
Klaas-Jan de Bruin - kjdbruin
WIllem van 't Veld - wvtveld

the code that i have is at follows

[CmdletBinding()]
param (
[Parameter(Mandatory=$True,position=1)]
[string]$firstname,

[Parameter(Mandatory=$True,position=2)]
[string]$lastname

 )

$name = $firstname.Trim() + " “+ $lastname.Trim()
$alias = ($firstname.Trim().Substring(0,1).ToLower() + $lastname.Trim().ToLower()) -replace “\s”,”"
$upn = $alias + “@” + $DomainName

i could add a extra parameter called insertion for the following words in the example :
van der
van 't
de

How can i solve this issue with perheps regular exressions or any other way.

thanks

It’s a bit hacky but here’s one solution

$string1 = "Klaas-Jan de Bruin"# – kjdbruin
$string2 = "WIllem van 't Veld" # – wvtveld

$string1,$string2 | % {

    # get last name
    $lastname = $_.split(" ")[-1]

    # strip last name from name
    $firstname = $_ -replace "$lastname`$",""

    # replace unwanted characters with placeholders
    $firststep = $firstname.trim() -replace "(\s+|-|')", "_"
    
    # replace multiple placeholders with one placeholder
    $secondstep =  ($firststep -replace "_+","_")
    
    # split to pick first character of each segment
    $thirdstep = ($secondstep.split("_") | % { $_[0]}) -join ""

    ($thirdstep + $lastname).ToLower()
}

Thanks
I will give it a try :slight_smile:
I let you know if it workes…,properly it will :slight_smile:

Works like a sharm…great :slight_smile:
thanks again

After some testing i have one issue left.
the thing is that the firstname, that is use to fill the firstname field in the AD, is now populated with the firstname and middle innitials. is there a way to get only the firstname.

something like string | foreach {
$firstname = $_.split(" ")[0]

$string1 = "Klaas-Jan de Bruin"# – kjdbruin
$string2 = "WIllem van 't Veld" # – wvtveld

Function New-MyUser
{
    Param($String)
    
    # get last name
    $lastname = $string.split(" ")[-1]

    # strip last name from name
    $firstname = $string -replace "$lastname`$",""

    # replace unwanted characters with placeholders
    $firststep = $firstname.trim() -replace "(\s+|-|')", "_"
    
    # replace multiple placeholders with one placeholder
    $secondstep =  ($firststep -replace "_+","_")
    
    # split to pick first character of each segment
    $thirdstep = ($secondstep.split("_") | % { $_[0]}) -join ""

    

    New-Object psobject -Property @{
        FirstName = $String.split(" ")[0]
        LastName = $lastname
        ShortName = ($thirdstep + $lastname).ToLower()
    }

}

New-MyUser $string1
New-MyUser $string2

thanks again