How to configure IPv4 address Windows 2022 using PowerShell
Follow below steps to configure IPv4 address and other TCP/IP settings, Default Gateway address and DNS Server addresses, in Windows Server 2022, using PowerShell cmdlets.
Step 1 - You must run PowerShell as administrator, to configure IPv4 address in Windows Server 2022. To run PowerShell as administrator, create a Power Shell shortcut on your Windows Server desktop, right-click the PowerShell shortcut icon and then select "Run as administrator", as shown below.
If you using a ServerCore based server, there is no desktop to create PowerShell shortcut. In ServerCore based server, use "runas" commmand from cmd or PowerShell, to run PowerShell as administrator. Enter the password of administrator, when prompted.
Step 2 - Now we need to identify the InterfaceIndex number of the network adapter, where you are going to configure IPv4 address. Use Get-NetAdapter PowerShell cmdlet as shown below to get the InterfaceIndex number of the network adapter where you want to configure IPv4 address.
Get-NetAdapter | Format-List
There can be many network adapters in a computer. As you can see from above output, the InterfaceIndex number is 3. We need to properly identify the network adapter and note it’s InterfaceIndex number. Every time when a PowerShell cmdlet related with this network adapter is run, we need to provide this number.
Step 3 - Once we had identified the network adapter and its InterfaceIndex number, we can use PowerShell "New-NetIPAddress" cmdlet to configure a new IPv4 address and Default Gateway address. You can see from the below PowerShell cmdlet, you need to provide InterfaceIndex number when you run the cmdlet, to identify the network adapter.
From the below cmdlet, you can understand that the IPv4 address we are configuring now is 192.168.10.203, subnet mask is 255.255.255.0 (-PrefixLength 24) and Default Gateway address we are configuring is 192.168.10.1. Change it as per your requirement.
New-NetIPAddress -InterfaceIndex 3 -IPAddress 192.168.10.203 -PrefixLength 24 -DefaultGateway 192.168.10.1
Step 4 - Now we have configured a new IPv4 address and Default Gateway address for the network adapter with InterfaceIndex number 3. To check whether the IPv4 address is configured properly, use PowerShell "Get-NetIPAddress" cmdlet as shown below.
Get-NetIPAddress -InterfaceIndex 3 -AddressFamily IPv4
From the above cmdlet output, we can see that the IPv4 address configured is 192.168.10.203. Now, to check whether Default Gateway address is configured properly, use "Get-NetRoute" command as shown below.
Get-NetRoute -InterfaceIndex 3 -AddressFamily IPv4 -DestinationPrefix "0.0.0.0/0"
From the above cmdlet output, we can see that the Default Gateway address configured is 192.168.10.1.
Step 5 - Last TCP/IP setting we are going to configure is DNS server addresses. Use "Set-DnsClientServerAddress" PowerShell cmdlet to configure DNS Server addresses, as shown below.
Set-DnsClientServerAddress -InterfaceIndex 3 -ServerAddresses 8.8.8.8, 8.8.4.4
Step 6 - To check whether the DNS Server addresses are configured properly, use PowerShell "Get-DnsClientServerAddress" cmdlet as shown below.
Get-DnsClientServerAddress -InterfaceIndex 3 -AddressFamily IPv4