Strip name to use for AD

Welcome Forums General PowerShell Q&A Strip name to use for AD

This topic contains 6 replies, has 2 voices, and was last updated by

 
Participant
3 years, 2 months ago.

  • Author
    Posts
  • #30425

    Participant
    Points: 1
    Rank: Member

    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

  • #30429

    Participant
    Points: 0
    Rank: Member

    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()
    }
    
  • #30434

    Participant
    Points: 1
    Rank: Member

    Thanks
    I will give it a try 🙂
    I let you know if it workes...,properly it will 🙂

  • #30435

    Participant
    Points: 1
    Rank: Member

    Works like a sharm...great 🙂
    thanks again

  • #30445

    Participant
    Points: 1
    Rank: Member

    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]

  • #30446

    Participant
    Points: 0
    Rank: Member
    $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
    
  • #30447

    Participant
    Points: 1
    Rank: Member

    thanks again

The topic ‘Strip name to use for AD’ is closed to new replies.