How to Set IP Address with PowerShell

An IP address is a unique number used to identify devices on a network. IP addresses can be static or dynamic. A static IP address is a fixed and unchanging IP address, while a dynamic IP address is an IP address that can change according to availability and demand.

Dynamic IP addresses are usually managed by DHCP (Dynamic Host Configuration Protocol), which is a service that automatically assigns IP addresses to devices that request them.

In this article, Bardimin will discuss how to set an IP address with PowerShell, both statically and dynamically. PowerShell is a powerful and flexible scripting and configuration management tool that can be used to automate a variety of administrative tasks. PowerShell can be used to set the IP address by using the cmdlets (commands) provided.

PowerShell IP Address

Prerequisite

Before we begin, make sure that you have the following:

$PSVersionTable.PSVersion
  • Administrator permissions to change network settings. You can run PowerShell as administrator by right-clicking on the PowerShell icon and selecting Run as administrator.
  • The name of the network interface for which you want to change the IP address. You can find out the name of your network interface by running the following command in PowerShell:
Get-NetAdapter

This command will display a list of network interfaces that exist on your computer, along with their names, status, and other information. Note the name of the network interface for which you want to change the IP address. In this example, we’ll use a network interface named Ethernet.

How to Set a Static IP Address with PowerShell

A static IP address is an IP address that does not change and must be manually assigned by the user. Static IP addresses are useful if you want to make sure that your devices can always be reached with the same IP address, or if you want to avoid IP address conflicts with other devices on the network.

To set a static IP address with PowerShell, follow these steps:

  1. Specify the IP address, subnet mask, and gateway you want to use. Make sure that the IP address you choose is not used by other devices on the network, and matches the IP address range specified by your router or DHCP server. A subnet mask is a value that determines the size and structure of your network, while a gateway is the IP address of a router or device that connects your network with other networks. In this example, we will use the IP address 192.168.1.100, subnet mask 255.255.25.0, and gateway 192.168.1.11.
  2. Run the following command in PowerShell to set a static IP address:
New-NetIPAddress -InterfaceAlias “Ethernet” -IPAddress 192.168.1.100 -PrefixLength 24 -DefaultGateway 192.168.1.1

This command will create a new IP address object with the specified parameters. The -InterfaceAlias parameter specifies the name of the network interface for which you want to change the IP address, the -IPAddress parameter specifies the IP address you want to use, the -PrefixLength parameter specifies the length of the subnet mask in bits (in this example, 24 bits means the subnet mask is 255.255.255.0), and the -DefaultGateway parameter specifies the gateway you want to use.

  1. Run the following command in PowerShell to set the DNS server you want to use:
Set-DnsClientServerAddress -InterfaceAlias “Ethernet” -ServerAddresses 8.8.8.8, 8.8.4.4

This command changes the DNS server settings for the specified network interface. The -ServerAddresses parameter specifies the IP addresses of the DNS server you want to use, separated by commas. In this example, we use Google’s DNS servers, which are 8.8.8.88 and 8.8.4.4.

You can use other DNS servers according to your preferences.

  1. Run the following command in PowerShell to check if the static IP address has been assigned successfully:
Get-NetIPAddress -InterfaceAlias “Ethernet”

This command displays information about the IP address assigned to the specified network interface. You can see that the IP address, subnet mask, and gateway match what you specified earlier.

How to Set a Dynamic IP Address with PowerShell

A dynamic IP address is an IP address assigned automatically by a DHCP server, which is a service that manages the distribution of IP addresses on the network. Dynamic IP addresses are useful if you don’t want to bother setting IP addresses manually, or if you frequently switch networks. To set a dynamic IP address with PowerShell, follow these steps:

  1. Run the following command in PowerShell to remove any static IP addresses that may have been previously assigned:
Remove-NetIPAddress -InterfaceAlias “Ethernet” -Confirm:$false

This command deletes the existing IP address object for the specified network interface. The -Confirm:$false parameter disables the confirmation normally requested by PowerShell before deleting the object.

  1. Run the following command in PowerShell to enable DHCP for the specified network interface:
Set-NetIPInterface -InterfaceAlias “Ethernet” -Dhcp Enabled

This command changes the DHCP settings for the specified network interface. The -Dhcp Enabled parameter specifies that DHCP should be enabled for that network interface.

  1. Run the following command in PowerShell to request a new IP address from the DHCP server:
Renew-NetIPAddress -InterfaceAlias “Ethernet”

This command will update the IP address for the specified network interface by requesting a new IP address from the DHCP server. You can use this command if you want to replace your IP address with another available one.

  1. Run the following command in PowerShell to check if the dynamic IP address has been assigned successfully:
Get-NetIPAddress -InterfaceAlias “Ethernet”

This command displays information about the IP address assigned to the specified network interface. You can see that the IP address, subnet mask, and gateway match those provided by the DHCP server.

In this article, we’ve discussed how to set an IP address with PowerShell, either statically or dynamically. We’ve also seen some PowerShell commands that are useful for managing network settings in Windows. Using PowerShell, you can set IP addresses easily and quickly, without having to open the Control Panel or Network and Sharing Center. Hope this article was helpful for those of you who want to learn more about PowerShell and networking. Thanks for reading.

RELATED ARTICLES

Latest Articles