I want to add my ip to system/user environment in a bat file.

This is the code of the bat file

curl http://httpbin.org/ip

This is the output:

   "origin": ""

I want to set environment set ip=

How should I do that?

  • Seeing as you tagged your question Windows7, I think you should really consider using Powershell instead of batch. It is a more modern and powerful shell native to Windows. – Aaron Jan 15 '16 at 13:51
  • Do you think ps, can make it eaiser to accomplish this task? – Aminadav Glickshtein Jan 15 '16 at 13:52
  • I really haven't used Powershell that much, but from what I've seen it's going from the 80's code madness that is batch to some modern code API trying to put at ease both bash and .NET users. If you've worked 20 years in batch I suppose there's no use learning PS now, but any less than that should quickly have a huge increase in productivity – Aaron Jan 15 '16 at 13:56
  • @Aaron I've only been learning batch for a short time, but I know what is easy to achieve. It's a much smaller language, so easier to learn and know its limitations and trickery. – DavidPostill Jan 15 '16 at 14:03
  • @Aaron I can answer questions like this in a few minutes. To learn the powershell equivalent would take me a lot longer. – DavidPostill Jan 15 '16 at 14:04

I want to add my IP address to the system/user environment in a bat file.

curl http://httpbin.org/ip

This is the output:

   "origin": ""

Use the following batch file (test.cmd):

rem @echo off
setlocal enableDelayedExpansion
for /f "usebackq skip=1 tokens=2" %%i in (`curl http://httpbin.org/ip`) do (
  setx ip %%~i
  goto :done


  • skip=1 is used to skip the first line {.
  • goto :done is use to skip the last line }.
  • ~ is used to remove the quotes from the IP address.

Further Reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
  • for /f - Loop command against the results of another command.
  • parameters - A command line argument (or parameter) is any value passed into a batch script.
  • setx - Set environment variables permanently, SETX can be used to set Environment Variables for the machine (HKLM) or currently logged on user (HKCU).
| improve this answer | |
  • Why enableDelayedExpansion? – Aminadav Glickshtein Jan 15 '16 at 13:45
  • 1
    @Amina Required for most batch thingies. Doesn't have anything specifically to do with this. – Insane Jan 15 '16 at 13:48
  • I was testing my batch file with set instead of setx and I had echo !ip! after set, which requires enableDelayedExpansion. If it works without you can remove it. – DavidPostill Jan 15 '16 at 13:48
  • Thanks. I removed it, and change it to set ip= – Aminadav Glickshtein Jan 15 '16 at 13:49
  • 1
    set ip= is temporary ... and will remain set only for the duration of the current cmd shell. Your question said "system/user environment" which I understood to mean you wanted it permanently set. – DavidPostill Jan 15 '16 at 13:54

This would be the equivalent in PowerShell :

$ip = curl http://httpbin.org/ip | Select-String -Pattern "[0-9.]+" | Select Matches
Set ip $ip.Matches.Get(0).Value
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.