Skip to main content

PowerShell: Configure Network Interface

Static Configuration

Get-NetIPConfiguration -Detailed | 
Select-Object InterfaceAlias, AllIPAddresses, @{Name='DnsServer'; Expression = {$_.DNSServer.ServerAddresses}} | 
Format-List

List current network configurations

Must be run as Administrator to configure network interface

$adapterName = "Ethernet 3"
$ipAddress = "192.168.0.50"
$networkMask = 24
$gateway = "192.168.0.1"
$dns = "192.168.0.1", "1.1.1.1"

Set-NetIPInterface -InterfaceAlias $adapterName -Dhcp Disabled
$newNetIpParameters = @{
  'InterfaceAlias' = $adapterName
  'IPAddress' = $ipAddress
  'PrefixLength' = $networkMask
  'DefaultGateway' = $gateway
  'Policy' = 'ActiveStore' # Remove param if you want to persist reboots
}
New-NetIPAddress @newNetIpParameters
Set-DnsClientServerAddress -InterfaceAlias $adapterName -ServerAddresses $dns

DHCP

Get-NetIPConfiguration -Detailed | 
Select-Object InterfaceAlias, AllIPAddresses, @{Name='DnsServer'; Expression = {$_.DNSServer.ServerAddresses}} | 
Format-List

List current network configurations

Must be run as Administrator to configure network interface

$adapterName = "Ethernet 3"
$ipAddress = '192.168.0.50'
$gateway = '192.168.0.1'

Get-NetIPAddress -IPAddress $ipAddress | Remove-NetIPAddress
Get-NetRoute -NextHop $gateway | Remove-NetRoute
Set-NetIPInterface -InterfaceAlias $adapterName -Dhcp Enabled
Set-DnsClientServerAddress -InterfaceAlias $adapterName -ResetServerAddresses
Restart-NetAdapter -InterfaceAlias $adapterName