PUBLIC IP – A PowerShell One Liner To Output to a File

The below is one line of PowerShell to output a Windows computers current public IP address to a text file along with a date time stamp.

I have previously triggered the script via a scheduled task to run each time a computer starts. The text file is appended with the new public IP and the date and time each time the script runs. 

You may want to update the two reference to C:\MyScripts\PublicIP.txt and the reference to C:\MyScripts with the location where you want to output to. The user the script is running as will need write access to this location.

  • The scripts first creates the folder C:\MyScript if it doesn’t already exists.
  • It then creates or appends the current date and time to the text file
  • Finally it appends the current public IP to the text file. The IP is determined by performing a REST API call to https://ipinfo.io/

powershell "New-Item -ItemType Directory -Force -Path C:\MyScripts; (Get-Date) | Out-File """C:\MyScripts\PublicIP.txt""" -Force -Append"; "Invoke-RestMethod ipinfo.io/ip | Out-File """C:\MyScripts\PublicIP.txt""" -Force -Append"

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share

Please consider sharing.