#How to set network IP Static/DHCP using Powershell in Windows 8 and above
This is a powershell script to set IP address for a network adapter using Powershell and restart the network adapter after it completes. Follow the below steps and detailed information about the parameters below.
Create a ps1
file using the below code
# Author: Munim ([email protected])
param (
[string]$Name = "Ethernet",
[IPAddress]$IP = "192.168.0.100",
[string] $CIDR = 24, # This means subnet mask = 255.255.255.0,
[string]$Gateway = "192.168.0.1",
[string]$Dns = "8.8.8.8,8.8.4.4",
[string]$IPType = "IPv4",
[string]$Type = "DHCP"
)
# Retrieve the network adapter that you want to configure
$adapter = Get-NetAdapter | ? {$_.Name -eq $Name}
if ($Type -eq "Static") {
# Remove any existing IP, gateway from our ipv4 adapter
If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) {
Write-Host "Removing existing IP"
$adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false
}
If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) {
Write-Host "Removing existing gateway"
$adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false
}
# Configure the IP address and default gateway
Write-Host "Configuring new IP"
$adapter | New-NetIPAddress `
-AddressFamily $IPType `
-IPAddress $IP `
-PrefixLength $CIDR `
-DefaultGateway $Gateway
# Configure the DNS client server IP addresses
Write-Host "Configuring new gateway"
$adapter | Set-DnsClientServerAddress -ServerAddresses $DNS
}
else {
$interface = $adapter | Get-NetIPInterface -AddressFamily $IPType
If ($interface.Dhcp -eq "Disabled") {
# Remove existing gateway
Write-Host "Removing existing gateway"
If (($interface | Get-NetIPConfiguration).Ipv4DefaultGateway) {
$interface | Remove-NetRoute -Confirm:$false
}
# Enable DHCP
Write-Host "Enabling DHCP on interface"
$interface | Set-NetIPInterface -DHCP Enabled
# Configure the DNS Servers automatically
Write-Host "Enabling automatic DNS"
$interface | Set-DnsClientServerAddress -ResetServerAddresses
}
}
Write-Host "Restarting adapter"
$adapter | Restart-NetAdapter
You need to invoke the script with the below parameters and details as follows:
[Parameter] Name
Name of the adapter you want to make changes to. Default is Ethernet
.
[Parameter] IP
The new IP address you're going to set. This parameter is ignored when DHCP is set. Default is 192.168.0.100
[Parameter] CIDR
This is CIDR (Classless Inter-Domain Routing), another form of subnet in simple terms of usage in this script. If CIDR is unknown, please visit http://mxtoolbox.com/subnetcalculator.aspx and put the according value after /
of your subnet. For instance, if your subnet is 225.225.252.0/22
, then your CIDR is 22 and hence your value for this script. The default value is 24, which is equivalent to 255.255.255.0
[Parameter] Dns
This is the default gateway for your network adapter you would like to set. Default is 192.168.0.1
[Parameter] IPType
The acceptable value is either IPv4
or IPv6
. For most people, you wouldn't need to set this, as it defaults to IPv4
.
[Parameter] Type
This is to set either Static
/DHCP
. Default is DHCP
.