PowerShell Function to Validate Both IPv4 and IPv6 Addresses

Last week, I wrote a blog article about the IP address type accelerator in PowerShell not being 100% accurate and I demonstrated using it for parameter validation. If you want to simply validate an IP Address and return a Boolean, that’s a little more complicated so I decided to write a function to perform that task along with providing detailed information about the IP address when an optional detailed parameter is specified:

#Requires -Version 2.0
function Test-MrIpAddress {

    Tests one or more IP Addresses to determine if they are valid.

    Test-MrIpAddress is an advanced function that tests one or more IP Addresses to determine if
    they are valid. The detailed parameter can be used to return additional information about the IP.

    One or more IP Addresses to test. This parameter is mandatory.

    Switch parameter to return detailed infomation about the IP Address instead of a boolean.

     Test-MrIpAddress -IpAddress '', ''

     Test-MrIpAddress -IpAddress '' -Detailed

     '::1', '' | Test-MrIpAddress



    Author:  Mike F Robbins
    Website: http://mikefrobbins.com
    Twitter: @mikefrobbins

    param (



        foreach ($Ip in $IpAddress) {

            try {
                $Results = $Ip -match ($DetailedInfo = [IPAddress]$Ip)
            catch {
                Write-Output $false

            if (-not($PSBoundParameters.Detailed)){
                Write-Output $Results
            else {
                Write-Output $DetailedInfo




The IpAddress parameter can accept one or more IP addresses. By default a Boolean is returned but specifying the detailed parameter returns more information if a valid IP address is specified. IP addresses are also accepted via pipeline input:

Test-MrIpAddress -IpAddress '', ''
Test-MrIpAddress -IpAddress '' -Detailed
'::1', '' | Test-MrIpAddress


The Test-MrIpAddress function shown in this blog article can be downloaded from my PowerShell repository on GitHub.