← script library

Get the Public IP Address with PowerShell

System AdministrationAugust 17, 2026

Asks three independent lookup services in turn until one answers with something that looks like an IPv4 address, so a single dead endpoint does not fail the check. The result is handed to the next runbook step, ready to be compared against the egress address a firewall rule or a SaaS allowlist expects.

powershell
# Get Public IP Address
# Single use case: report the WAN IP this server egresses from
#-----------------------------------------------------------------

function Write-Log {
    param($Message)
    Write-Host "<WRITE-LOG = `"*$Message*`">"
}

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$ip = $null
foreach ($endpoint in @("https://api.ipify.org", "https://ifconfig.me/ip", "https://icanhazip.com")) {
    try {
        $ip = (Invoke-RestMethod -Uri $endpoint -TimeoutSec 10 -UseBasicParsing).ToString().Trim()
        if ($ip -match "^\d{1,3}(\.\d{1,3}){3}$") {
            Write-Log "Public IPv4: $ip (via $endpoint)"
            break
        }
        $ip = $null
    } catch { continue }
}

if ($null -eq $ip) {
    Write-Log "Could not determine the public IP - no lookup endpoint reachable (proxy/firewall?)."
    return
}

# Pass to the next runbook script (ex. compare against expected egress IP)
$store = $ip

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.