fbpx

How To Use Windows Powershell For IP Address Configuration?

PowerShell can also be used for IP address configuration, enabling users to manage network settings efficiently. In this article, we will guide you through the process of using Windows PowerShell to configure IP addresses on your Windows-based computer.

How To Use Windows Powershell For IP Address Configuration? 

Windows PowerShell is a powerful command-line shell and scripting language developed by Microsoft to automate various tasks and configurations on Windows operating systems. Among its wide range of applications.

Getting Network Adapter Properties (Get-NetAdapter)

To use Windows PowerShell for IP address configuration, follow these steps:

  • Open Windows PowerShell: Press Win + X, then select “Windows PowerShell” from the menu. If you need administrative privileges, choose “Windows PowerShell (Admin).
  • Check the current network adapter configuration: Type the following command and press Enter to see a list of network adapters and their current configurations:
Get-NetAdapter | Select-Object Name, InterfaceIndex, Status, MacAddress
Get-NetAdapter | Select-Object Name, InterfaceIndex, Status, MacAddress
  • Note down the name or InterfaceIndex of the network adapter you want to configure.
  • Set a static IP address: If you want to assign a static IP address to the network adapter, use the following command:
New-NetIPAddress -InterfaceIndex  -IPAddress  -PrefixLength  -DefaultGateway
  • Replace the InterfaceIndex of your network adapter,  with the desired IP address you want to assign (e.g., “192.168.1.100”),  with the subnet prefix length (e.g., “24” for a typical home network with subnet mask 255.255.255.0), and with the gateway address (usually the router’s IP address).

Example:

New-NetIPAddress -InterfaceIndex 1 -IPAddress 192.168.1.100 -PrefixLength 24 -DefaultGateway 192.168.1.1

Set DNS servers: To configure DNS servers, use the following command:

Set-DnsClientServerAddress -InterfaceIndex  -ServerAddresses ("", "")

Replace with the InterfaceIndex of your network adapter,  and with the preferred and alternate DNS server addresses, respectively. Separate multiple DNS server addresses with commas.

Example:

Set-DnsClientServerAddress -InterfaceIndex 1 -ServerAddresses ("8.8.8.8", "8.8.4.4")
  • Verify the changes: To check if the changes have been applied successfully, you can run the following commands to view the adapter configuration:
Get-NetIPAddress

Get-DnsClientServerAddress
  • Ensure that the IP address, subnet mask, default gateway, and DNS server information are correct.
  • Disable DHCP: If you want to disable DHCP (Dynamic Host Configuration Protocol) on the network adapter to prevent automatic IP address assignment, you can use the following command:
Set-NetIPInterface -InterfaceIndex  -DHCP Disabled
  • Replace with the InterfaceIndex of your network adapter.

Remember that making changes to your network adapter configuration can cause network connectivity issues if not done correctly. Be cautious when configuring IP addresses, and ensure you have the correct details provided by your network administrator or Internet Service Provider (ISP) if applicable. If you encounter any issues, you can revert to obtaining IP addresses automatically through DHCP by setting the -DHCP Enabled option.

Set Static IP Address Using PowerShell In Windows 10

Let’s attempt to give the NIC a static IP address. Use the following command to modify an Ethernet network interface’s default gateway, network mask, and IP address:

Get-NetAdapter -Name Ethernet0| New-NetIPAddress –IPAddress 192.168.2.50 -DefaultGateway 192.168.2.1 -PrefixLength 24

An array structure can be used to set an IP address (more visually):

$ipParams = @{
InterfaceIndex = 8
IPAddress = "192.168.2.50"
PrefixLength = 24
AddressFamily = "IPv4"
}
New-NetIPAddress @ipParams

Use the Set-NetIPAddress cmdlet to modify a static IP address that has already been configured:

Set-NetIPAddress -InterfaceAlias Ethernet0 -IPAddress 192.168.2.90

Run the following command to stop your adapter’s DHCP server from assigning it an IP address:

Set-NetIPInterface -InterfaceAlias Ethernet0 -Dhcp Disabled
Remove static IP address:
Remove-NetIPAddress -IPAddress "xxx.xxx.xxx.xxx"

Getting the Address IP configuration in PowerShell with Get-NetIPAddress

You used a single command to previously list every network interface. However, the Get-NetIPAddress cmdlet also allows you to list property values and your host’s current IP address.

Run the following cmdlet (without any parameters) to see the addresses and PowerShell IP settings for each adapter.

Get-NetIPAddress

In order to limit the output (Select-Object) to the Ethernet’s InterfaceAlias and IPAddress, perform the following Windows command.

Get-NetIPAddress -InterfaceAlias Ethernet | Select-Object -Property InterfaceAlias,IPAddress
Get-NetIPAddress -InterfaceAlias Ethernet | Select-Object -Property InterfaceAlias,IPAddress

Obtaining IP Route Data (Get-NetRoute)

A host can communicate with other hosts on the same network thanks to its IP address. You require a gateway address, and the Get-NetRoute cmdlet is what you require. Information that earlier cmdlets you have learned about do not provide is provided by this one.

Use the Get-NetRoute command shown below to examine the Ethernet interface’s current gateway (NextHop).

Get-NetRoute -InterfaceAlias Ethernet | Where-Object -Property RouteMetric -EQ 0 | Select-Object -Property NextHop

Normally, the gateway address is provided by a DHCP server.

Obtaining the IP addresses of DNS servers (Get-DnsClientServerAddress)

Both internal and external networks need DNS to resolve names. Without DNS, you would have to keep track of each host’s internet address on your network.

However, it’s likely that your host has already set up DNS using DHCP. The Get-DnsClientServerAddress cmdlet can be used to check the configuration at any time.

Get-DnsClientServerAddress
Get-DnsClientServerAddress

By sending the output to a text file or writing down the IP addresses, you can save them. 

(Get-NetIPConfiguration) Obtaining IP Network Configurations

The Get-NetIPConfiguration cmdlet will work when you need all three of the previously taught cmdlets’ information at once. The main advantage of this cmdlet is that it gives more precise information.

To view the IP network configurations acquired by the previous cmdlets you executed, run the Get-NetIPConfiguration cmdlet below.

Get-NetIPConfiguration
Get-NetIPConfiguration