PowerShell IP Address Type Accelerator Not 100% Accurate

Testing whether or not an IP Address is valid with PowerShell is a fairly simple process with the [ipaddress] type accelerator if your goal is to validate input for a script or function as shown in the following code example:

When a valid IP address is specified, the function continues and when an invalid IP address is specified, the function terminates immediately and returns an error message:


The IP Address type accelerator can also be used to validate IPv6 addresses:


As you can see in the previous sets of results, input validation of an IP address for a script or function in PowerShell is an almost no code solution although I’ve seen others write an enormous amount of code to accomplish this task, but is the IP address type accelerator in PowerShell accurate?

What I discovered is the IP address type accelerator in PowerShell isn’t 100% accurate. Entering a number that’s five 2’s seems to be cast to a valid IP address so the type accelerator thinks it’s valid (clearly it isn’t):


One simple line of code can weed out those inaccurate results:


ValidateScript along with a little code can be used to prevent this scenario from occurring when performing input validation for IP addresses:

The IP address validation now works as expected as shown in the following example:


Please share your thoughts, suggestions, and/or comments via a comment to this blog article.


I also discovered that the same five digit number is translated to a valid IP address when using the ping command from the command prompt which completely eliminates PowerShell:


Maybe someone can explain why this occurs? See the comments to this blog article for the explanation.

The examples shown in this blog article were written and tested on Windows 10 Enterprise Edition running PowerShell version 5.1.14393.206.

Update #2:

There’s been lots of discussion about this blog article on Twitter, one interesting response caught my eye and is also an IP address that you wouldn’t think would be valid, but both the IP address accelerator in PowerShell and ping via the command prompt expand 10.1 as



The code used in my Test-ValidInput function previously shown in this blog article weeds out those unexpected results as well:


The results for integers like ‘22222’ and IP addresses like ‘10.1’ aren’t necessarily wrong, but unexpected if you’re trying to validate the input is a valid IP address specified in quad-dotted notation of four decimal integers.



  1. Mike Shepard

    Looks like it’s taking the int, splitting it into bytes, and then mapping them into octets.

    Given that a 32-bit integer and 4 8-bit octets take the same storage it kind of makes sense.

    22222 = 86*256 + 206

  2. Mike F Robbins

    Comment from Twitter:
    Comment from Twitter
    22222 is the integer equivalent of http://www.aboutmyip.com/AboutMyXApp/IP2Integer.jsp?ipAddress= … Ping must type cast the same as the accelerator?

  3. chrisjwarwick

    Hi Mike,

    22222 is a valid address – it’s just not in the usual “dotted-decimal” notation:-) You can see the equivalent dotted-decimal form in the IpAddressToString property of [IPAddress]. See https://technet.microsoft.com/en-us/library/dd379547 or https://en.wikipedia.org/wiki/IPv4 or the RFC https://tools.ietf.org/html/rfc791

    Talking of ‘seen others write an enormous amount of code to accomplish this task’, try my PsGallery thing https://www.powershellgallery.com/packages/IPv6Regex/1.1.1 – lots of code (but it was just for fun 🙂


  4. JB Lewis

    Code like this would get you an unsigned Int from a dotted decimal IP
    $DecimalIP = 0
    [IPAddress$IPAddress.GetAddressBytes()| Foreach-object {
    $DecimalIP += $_ * [math]::Pow(256,$i)


    Indeed, the reverse is what Ping is doing to come up with an address to ping.

    will a collection of [IPAddress] sort correctly? or does one have to convert them to decimal in order to get them to sort?


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: