Here’s a handy function you can use in your PowerShell scripts when you need to verify that information contains a valid emails address.
function ValidateEmailAddress {
param (
[string]$EmailAddress
)
$emailRegex = ‘^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$’
$isValid = $EmailAddress -match $emailRegex
return $isValid
}
Just call the function and specify the text you want to verify as a parameter like:
ValidateEmailAddress(“director@ciaops.com”)
and you’ll get either True or False.
some in depth article about [mailaddress] type casting can be found here:
validation – Powershell to Validate Email addresses – Stack Overflow
LikeLike