Functions/Data/Format-Username.ps1

Function Format-Username
{
    [CmdletBinding()]
    Param
    (
        # Incoming Username to Reformat
        [Parameter(Mandatory=$True,ValueFromPipeLine = $true)]
        [String]
        $Username,

        # Style of Formatting
        [Parameter(Mandatory=$False)]
        [ValidateSet("UPN","FSUPN","Legacy","Basename")]
        [String]
        $Style = "UPN"
    
    )
    Process
    {
        # Derive Outgoing UPN
        if ($Username -like "*\\*")
        {
            $NSplit = $Username -split '\\'
            $D = $NSplit[0]
            $UN = $NSplit[1]
        }
        elseif ($Username -like "*@*")
        {
            $NSplit = $Username -split '@'
            $D = $NSplit[-1]
            $UN = $NSplit[0]
        }
        elseif ($Username -like "*_*")
        {
            $NSplit = $Username -split '_'
            $D = $NSplit[-1]
            $UN = $NSplit[0]
        }

        # Return Outgoing UPN
        switch ($Style)
        {
            "UPN" {"$($UN)@$($D)"}
            "FSUPN" {"$($UN)_$($D)"}
            "Legacy" {"$($D)\$($UN)"}
            "Basename" {"$UN"}
        }       
    }
}