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:
-
Splitting the IP Address Octets and Typecasting to
[Byte]
A
byte
denotes an integral type that stores unsigned 8-bit integer values within range 0-255.[byte]::MaxValue
[byte]::MinValueand 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('.')
-
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 theregex
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 $patternBut this regex only works for IPv4 addresses and you can find one IPv6 as well after a little googling.
-
Type cast to
[System.Net.IPAddress]
ClassPowerShell Type accelerator
[IPAddress]
which is also an alias ofSystem.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 toSystem.Net.IPAddress
objects, then toBool
data type, so that if the IP Address is valid$true
would be returned, otherwise$false
-
Using
TryParse()
method ofSystem.Net.IPAddress
class.Net Class:
System.Net.IPAddress
has aTryParse()
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
- 4 ways to send output to NULL in PowerShell
- 3 ways to store and display PowerShell Variable simultaneously
- 4 ways to select Unique items in PowerShell
- 4 Ways to Create PowerShell Objects
- 2 Ways to Copy Files from Windows 10 to Windows Sub-System for Linux
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 Science, Artificial 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
Prateek Singh
Related posts
2 Comments
Leave a ReplyCancel reply
Categories
Author of Books
Awards
Open Sourced Projects
Author at
Blog Roll
Mike F RobbinsDamien Van Robaeys
Stéphane van Gulick
Kevin Marquette
Adam Bertram
Stephanos Constantinou
Francois-Xavier Cat
Ravikanth Chaganti
Roman Levchenko
Blog Stats
- 1,131,953 People Reached
Very Cool. I was looking for a canned regEX for powershell but I like the call out to validate the IP address and using the -as to return a True or False.
Thanks
-Richpo
[IPAddress] unfortunately seems very permissive. For instance [IPAddress]’192.168.1′ is interpreted as ‘192.168.0.1’! Perhaps the check could be augmented using something like “$input.Split(‘.’).Length -eq 4”.