Set NetIPAddress - PowerShell


Overview

Set-NetIPAddress is a powerful PowerShell command used to configure IP addresses for network interfaces. It allows for dynamic and efficient management of IP addresses, enabling network administrators to quickly adjust IP configurations based on varying requirements.

Syntax

Set-NetIPAddress [-InterfaceAlias] <String> [-AddressFamily] <AddressFamily> -IPAddress <String> [-PrefixLength] <UInt16> [-DefaultGateway] <String> [-DNSServer] <String[]> [-AlternateDNSServer] <String[]> [-Suffix] <String> [-GatewayMetric] <Int32>

Options/Flags

  • -InterfaceAlias: Specifies the network interface to configure.
  • -AddressFamily: Sets the address family to be configured. IPv4 or IPv6.
  • -IPAddress: The new IP address to be assigned to the interface.
  • -PrefixLength: The subnet prefix length for the new IP address.
  • -DefaultGateway: The default gateway for the interface.
  • -DNSServer: An array of DNS server addresses to be configured.
  • -AlternateDNSServer: An array of alternate DNS server addresses to be configured.
  • -Suffix: A suffix to be appended to the interface’s name after the IP address change.
    -GatewayMetric: Sets the metric for the default gateway.

Examples

Example 1: Assigning an IPv4 Address

Set-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4 -IPAddress "192.168.1.10" -PrefixLength 24

Example 2: Configuring a Full IP Configuration

Set-NetIPAddress -InterfaceAlias "Wi-Fi" -AddressFamily IPv4 -IPAddress "10.0.0.15" -PrefixLength 22 -DefaultGateway "10.0.0.1" -DNSServer "8.8.8.8" -AlternateDNSServer "8.8.4.4" -GatewayMetric 10

Common Issues

  • IPv6 Address Configuration: When configuring IPv6 addresses, ensure the interface alias specified supports IPv6 connectivity.
  • Subnet Prefix Validation: Verify that the provided subnet prefix length is within the allowed range (e.g., 0 for IPv4 and 64 for IPv6).
  • Duplicate Address: Avoid assigning duplicate IP addresses within the same network segment.

Integration

Integration with Get-NetIPAddress: Use Get-NetIPAddress to retrieve the current IP configuration, then pipe the output to Set-NetIPAddress to make changes.

Get-NetIPAddress -InterfaceAlias "Ethernet" | Set-NetIPAddress -IPAddress "192.168.1.20"
  • Get-NetIPAddress: Retrieves IP address configurations.
  • Remove-NetIPAddress: Removes configured IP addresses.
  • New-NetIPAddress: Creates new IP address configurations.