Use regular expressions
$ipRegEx="\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
if($ip -notmatch $ipRegEx)
{
}
You can search the Internet for regular expressions and examples for IP. Just keep in mind that powershell is built on top of .NET, so when searching and reading for regular expressions, focus on .NET or C #. For example this .
Update
As stated after the comments, the regular expression is incorrect, but it was published as an example of validating the expressions. An alternative could be((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]?)
source
share