Skip to content

Instantly share code, notes, and snippets.

@a-bode
Created February 6, 2020 16:01
Show Gist options
  • Save a-bode/eca026910738fb5189f0a596f72a973c to your computer and use it in GitHub Desktop.
Save a-bode/eca026910738fb5189f0a596f72a973c to your computer and use it in GitHub Desktop.
PowerShell: Validate Email or IPv4 Address using Regex
function Test-Address {
[CmdletBinding(DefaultParameterSetName = "EmailAddress")]
param (
[parameter(ParameterSetName = "EmailAddress")] $EmailAddress,
[parameter(ParameterSetName = "IPv4")] $IPv4
)
if ($EmailAddress) {
$patern = '^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$'
$DidMatch = $EmailAddress -match $patern
}
if ($IPv4) {
$patern = '^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$'
$DidMatch = $IPv4 -match $patern
}
if ($DidMatch) {
return $true
}
else {
return $false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment