How to Resolve IP Address From Hostname With PowerShell

Rohan Timalsina Feb 02, 2024
  1. Use the Resolve-DnsName Cmdlet to Resolve IP Address From Hostname With PowerShell
  2. Use the Dns.GetHostAddresses Method to Resolve IP Address From Hostname With PowerShell
How to Resolve IP Address From Hostname With PowerShell

A hostname, such as www.example.com, identifies a website or host on the internet. IP addresses are assigned to host names.

Sometimes you may need to get an IP address from the hostname or vice-versa. It can be easily done with the help of PowerShell.

This tutorial will teach you to resolve the IP address from hostname or vice-versa using PowerShell.

Use the Resolve-DnsName Cmdlet to Resolve IP Address From Hostname With PowerShell

The Resolve-DnsName cmdlet performs a DNS name query resolution for the specified name.

The following command resolves a hostname delftstack.com.

Resolve-DnsName delftstack.com

Output:

Name                                           Type   TTL   Section    IPAddress
----                                           ----   ---   -------    ---------
delftstack.com                                 A      60    Answer     3.6.118.31
delftstack.com                                 A      60    Answer     3.6.18.84

As you can see, it prints other information along with the IP address. To get only IP addresses, use the following command.

(Resolve-DnsName delftstack.com).IPAddress

Output:

3.6.18.84
3.6.118.31

To resolve a hostname from the IP address, you can specify an IP address to the command.

Resolve-DnsName 3.6.118.31

Output:

Name                           Type   TTL   Section    NameHost
----                           ----   ---   -------    --------
31.118.6.3.in-addr.arpa        PTR    300   Answer     ec2-3-6-118-31.ap-south-1.compute.amazonaws.com

Use the Dns.GetHostAddresses Method to Resolve IP Address From Hostname With PowerShell

The GetHostAddresses method of Dns Class displays the IP addresses of the specified host.

The following example returns the IP addresses of the host delftstack.com.

[System.Net.Dns]::GetHostAddresses('delftstack.com')

Output:

Address            : 1410467331
AddressFamily      : InterNetwork
ScopeId            :
IsIPv6Multicast    : False
IsIPv6LinkLocal    : False
IsIPv6SiteLocal    : False
IsIPv6Teredo       : False
IsIPv4MappedToIPv6 : False
IPAddressToString  : 3.6.18.84

Address            : 527828483
AddressFamily      : InterNetwork
ScopeId            :
IsIPv6Multicast    : False
IsIPv6LinkLocal    : False
IsIPv6SiteLocal    : False
IsIPv6Teredo       : False
IsIPv4MappedToIPv6 : False
IPAddressToString  : 3.6.118.31

It displays additional information like Address, AddressFamily, ScopeID, and others. To print only IP addresses, run the command below.

[System.Net.Dns]::GetHostAddresses('delftstack.com').IPAddressToString

Output:

3.6.118.31
3.6.18.84

The GetHostEntry method resolves a hostname from the IP address.

[System.Net.Dns]::GetHostEntry('3.6.118.31')

Output:

HostName                                        Aliases AddressList
--------                                        ------- -----------
ec2-3-6-118-31.ap-south-1.compute.amazonaws.com {}      {3.6.118.31}

We hope this tutorial gave you an idea to resolve an IP address from the hostname or vice-versa in PowerShell.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website