Get IP address on Windows 7 via command line

4.9k Views Asked by At

I have a list of aliases defined in a command file (aliases.cmd) that I load whenver I run a command prompt (cmd.exe /k aliases.cmd). Using doskey, I have several aliases defined and I am trying to create one to show only my IP address. I came across this question with a lot of good ways to get an IP in a batch file but none of them seem to work via doskey. For example, I have this alias defined:

doskey ip=ipconfig | findstr /R /C:"IPv4 Address"

When I run it via the command prompt (excluding the doskey portion), it works and only returns the IPv4 address. However, via doskey and the "ip" keyword, it does not work and returns the regular output of "ipconfig".

Is there any way to get an IP address (and only the IP) on Windows via a doskey alias?

2

There are 2 best solutions below

0
On BEST ANSWER

So after some study, the only way I could get this to work is the following.

doskey ip=ip.cmd

And ip.cmd has the following.

@echo off
ipconfig | findstr /R /C:"IPv4 Address"
1
On

I believe your problem was that you did not escape the pipe character, as with: doskey ip=ipconfig ^| findstr /R /C:"IPv4 Address"