Functions/Get-EmailAddressUsername.ps1

<#
.SYNOPSIS
    This function extracts the username portion of an email address.
.DESCRIPTION
    This function extracts the username portion of an email address.
    The domain is the portion of the email before the '@' character.
    If the email is invalid, an empty string is returned.
.EXAMPLE
    Get-EmailAddressUsername -EmailAddress "mailbox@domain.com"
#>

function Get-EmailAddressUsername {
    [CmdletBinding()]
    [OutputType([String])]
    param (
        # The email address which the domain will be extracted from.
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$emailAddress
    )

    # Check email validity
    if (!(Test-EmailAddressValidity -EmailAddress $emailAddress)) {
        return ""
    }

    # Extract domain
    return $emailAddress -replace "^([\S]+)@[\S]*?$", "`$1"
}