0

I am trying to fetch the IP address of the machine.

[System.Net.Dns]::GetHostAddresses('testpc') | Select-Object -expand IPAddressToString

and it is returning the IPAddress but this machine has more than one network adaptor. So, it is returning 3 IP addresses.

10.4.6.10
192.168.100.8
::1

I need an IP address in a separate variable.

Any advise how can i assign it.

1 Answer 1

1

As Select-Object will return an array, if it returns more than one object, you can access each element by an index for example:

([System.Net.Dns]::GetHostAddresses('testpc') | Select-Object -expand IPAddressToString)[1]

Output:

192.168.100.8
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. How can i assign other IP address to variable. Because it may vary system to system.
What may vary? The IP addresses on each system (sure) or the number of IP addresses on each system?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.