Skip to content

Check-Ipv4Address

Checks an IPv4 address for validity

param([string]$Address = "")

function IsIPv4AddressValid { param([string]$IP)
	$RegEx = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
	if ($IP -match $RegEx) {
		return $true
	} else {
		return $false
	}
}

try {
	if ($Address -eq "" ) { $Address = read-host "Enter IPv4 address to validate" }

	if (IsIPv4AddressValid $Address) {
		"? IPv4 $Address is valid"
		exit 0 # success
	} else {
		write-warning "Invalid IPv4 address: $Address"
		exit 1
	}
} catch {
	"?? ERROR: $($Error[0]) (script line $($_.InvocationInfo.ScriptLineNumber))"
	exit 1
}

Specifies the IPv4 address to check

An interactive directory of PowerShell scripts.