This is how to get the current IP address of the active network adapter in your Windows system.
PS C:\Users\Doom> (Test-Connection -ComputerName (hostname) -Count 1).IPV4Address.IPAddressToString 192.168.1.2 |
This prints the IP address of your machine to the Powershell terminal. This is a great way to get just the IP address and no other data.
To print the IP address into a variable, use this one-liner.
PS C:\Users\Doom> $address = (Test-Connection -ComputerName (hostname) -Count 1).IPV4Address.IPAddressToString |
This allows retrieving that information with another command in a script. This is a good use of Powershell. Some of the syntax is annoying in Powershell, but it is alright once you get used to the way it works.
Print all IPv4 IP addresses.
PS C:\Users\Doom> Get-NetIPAddress -AddressFamily IPv4 | Out-String -stream | Select-String -Pattern "IPAddress" IPAddress : 192.168.147.1 IPAddress : 192.168.146.1 IPAddress : 169.254.235.249 IPAddress : 127.0.0.1 IPAddress : 192.168.1.2 |
Compared the commands above to how it is done in bash.
4.4 Wed May 27 jason@Yog-Sothoth 0: $ ip a | awk '/inet / { print $2 }' | sed -n 2p | cut -d "/" -f1 192.168.1.2 |
But this achieves the same result. To get your Internet-facing IP address, use this command below.
4.4 Wed May 27 jason@Yog-Sothoth 0: $ curl https://ifconfig.me/ip ; echo 203.240.220.112 |
This is how easy that is when using bash.