Validate IP Address in PowerShell

Often you will come across scenarios where you want to Validate IP Address In PowerShell as a parameter or user input, so today in this article we are discussing few methods to validate the IP Address:

  1. Splitting the IP Address Octets and Typecasting to [Byte]

    byte denotes an integral type that stores unsigned 8-bit integer values within range 0-255.

    [byte]::MaxValue
    [byte]::MinValue

    and an IP Address is 32-Bit number, or in other words 4 Octets represented each by a numerical label withing range 0-255 separated by a . (dot). So if we simply split a IP Address by . and we typecast individual number to [Byte] PowerShell Type accelerator, then it will throw an error if the number is out of range 0-255.

    [byte[]] '192.255.0.0'.split('.')

  2. Using a Regular expressions

    Regular expressions can be used to validate IP Addresses in PowerShell with the -match operator that will validate a pattern depending upon the regex passed to it, like in the following example: but this regex only works for IPv4 addresses:

    $pattern = "^([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}$"
    '192.168.0.0' -match $pattern

    But this regex only works for IPv4 addresses and you can find one IPv6 as well after a little googling.

  3. Type cast to [System.Net.IPAddress] Class

    PowerShell Type accelerator [IPAddress] which is also an alias of System.Net.IPAddress class can validate IP Addresses if it is typecast-ed against it, and will throw an error when IP Address is out of range or is incorrect in format.

    [IPAddress] '192.168.0.1'
    [IPAddress] '192.168.0.999'
    [IPAddress] '192.168.0.test'

    Alternatively, you can also use the -As operator to convert an IP Address to System.Net.IPAddress objects, then to Bool data type, so that if the IP Address is valid $true would be returned, otherwise $false

  4. Using TryParse() method of System.Net.IPAddressclass

    .Net Class: System.Net.IPAddress has a TryParse() method that can also validate a IP Address, like in the following code sample:

    # Valid IPAddress
    [ipaddress]::TryParse("192.168.1.145",[ref][ipaddress]::Loopback)
    # Invalid IPAddress
    [ipaddress]::TryParse("192.168.1.1145",[ref][ipaddress]::Loopback)

    But, there is a limitation with IPAddress.TryParse method, that is it verifies if a string could be converted to an IP address. So, if you pass a string “7”, it considered as “0.0.0.7” and $true would be returned.


If you like this article read more similar articles under ‘N – Ways to’ category


My New Book :  PowerShell Scripting Guide to Python

This PowerShell Scripting guide to Python is designed to make readers familiar with syntax, semantics and core concepts of Python language, in an approach that readers can totally relate with the concepts of PowerShell already in their arsenal, to learn Python fast and effectively, such that it sticks with readers for longer time.

“Use what you know to learn what you don’t. ” also known as Associative learning.

Book follows a comparative method to jump start readers journey in Python, but who is the target audience? and who should read this book –

  • Any System Administrator who want to step into Development or Programming roles, and even if you don’t want to be a developer, knowledge of another scripting language will make your skill set more robust.
  • Python Developers who want to learn PowerShell scripting and understand its ease of user and importance to manage any platform.

Python is one of the top programming languages and in fast changing IT scenarios to DevOps and Cloudto the future – Data ScienceArtificial Intelligence (AI) and Machine Learning Python is a must know.

But this PowerShell Scripting guide to Python would be very helpful for you if you already have some knowledge of PowerShell

NOTE! This is a Leanpub “Agile-published” book. That means the book is currently unfinished and in-progress. As I continue to complete the chapters, we will re-publish the book with the new and updated content. Readers will receive an email once a new version is published!

While the book is in progress, please review it and send any feedback or error corrections at prateek@ridicurious.com

Optical Character Recognition

Subscribe to our mailing list

* indicates required