Using the Invoke-RestMethod cmdlet, content can be queried from online geolocation providers to gather connection details such as public IP address and ISP location information. In this example I used a free service from ipecho.net to create the Get-PublicIP function. For older versions of PowerShell, you can use the Invoke-WebRequest cmdlet, but you must retrieve the IP address data from the object's content property.
The Get-PublicIP function returns the public IP address of the local computer running the command. The ipecho.net service has limitations in place, so review the developer notes on their website before using it in production.