Monday, 15 September 2025

Extracts the numeric value from the string. Trims the non-numeric part from the end so that when the number is appended, the total length is exactly 20 characters.

Extracts the numeric value from the string. Trims the non-numeric part from the end so that when the number is appended, the total length is exactly 20 characters.


function Format-MailboxName {
    param (
        [string]$InputName
    )
    # Extract numeric part (assumes it's at the end)
    $number = ($InputName -split '\D+')[-1]
    # Calculate how many characters to keep from the start
    $maxPrefixLength = 20 - $number.Length
    # Remove the number from the end and trim to required length
    $prefix = $InputName -replace "$number$", ''
    $trimmedPrefix = $prefix.Substring(0, [Math]::Min($prefix.Length, $maxPrefixLength))
    # Combine trimmed prefix and number
    $finalName = "$trimmedPrefix$number"
    return $finalName
}

# Example usage
Format-MailboxName "NewSharedMailboxDelhi-CWW13"