Check-ExternalIP.ps1

<#PSScriptInfo
 
.VERSION 2.0
 
.GUID 9ab7fe54-8fc0-4f06-bddc-05fca6f65e54
 
.AUTHOR Aaron Guilmette
 
.COMPANYNAME Microsoft
 
.COPYRIGHT 2021
 
.TAGS IPaddress check
 
.LICENSEURI
 
.PROJECTURI https://www.undocumented-features.com/2021/02/09/check-external-ip-function/
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
.DESCRIPTION
Retrieves external IP address.
 
.PRIVATEDATA
 
#>


<#
.SYNOPSIS
Check external IP address from PowerShell.
 
.PARAMETER Provider
Select which lookup provider to use.
 
.NOTES
2021-05-11 - Publish to PowerShell Gallery.
#>

Function Check-ExternalIP
{
    [cmdletbinding()]
    param (
        [Parameter()]
        [ValidateSet('dyndns', 'ipchicken', 'myipaddress')]
        [string]$Provider)
    
    switch ($Provider)
    {
        dyndns {
            $source = "DynDns.com"
            $ip = (Invoke-WebRequest -Uri http://checkip.dyndns.com).content -replace '[^\d\.]'
        }
        ipchicken {
            $source = "IPChicken.com"
            $data = (Invoke-WebRequest -Uri https://www.ipchicken.com).ParsedHtml.body.outerText.Split("`n")
            $ip = $data | ? { $_ -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" }
        }
        myipaddress {
            $source = "MyIPAddress.com"
            $data = (Invoke-WebRequest -Uri http://myipaddress.com).ParsedHtml.body.outerText.split("`n")
            $ip = $data | ? { $_ -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" }
        }
        default {
            $source = "DynDns.com"
            $ip = (Invoke-WebRequest -Uri http://checkip.dyndns.com).content -replace '[^\d\.]'
        }
    }
    Return $ip
}