Skip to content

Powershell: Check is a valid IP address

Last updated on July 11, 2023


To check if a string represents a valid IP address in PowerShell, you can use the System.Net.IPAddress class. Here’s an example:

$ipAddressString = "192.168.0.1"

$isValidIP = [System.Net.IPAddress]::TryParse($ipAddressString, [ref]$null)

if ($isValidIP) {
    Write-Host "The IP address '$ipAddressString' is valid."
}
else {
    Write-Host "The IP address '$ipAddressString' is not valid."
}

In this script, we assign the IP address string to the $ipAddressString variable. We then use the TryParse method of the System.Net.IPAddress class to attempt parsing the string as an IP address. The method returns a boolean value indicating whether the parsing was successful.

If the IP address string is valid, the script displays a message confirming its validity. Otherwise, it shows a message indicating that the IP address is not valid.

You can replace the $ipAddressString variable with the IP address string you want to check. Feel free to integrate this IP address validation logic into your existing scripts or modify it as per your specific requirements.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments