# Add- Remove IPv4 Address
# Written by WebBanshee
# Feel free to use.
#—————————————————
clear
# Display Net Adapters
$netAdapter = Get-NetAdapter
$netAdapter# Choose Netadapter to Add / Remove IP. Selection is based on Interface Index [ifIndex]
Write-Host “”
Write-Host -ForegroundColor Yellow “Type ifIndex value for the appropriate net adapter” -NoNewline
$ifIndex = read-host ” ”
# Display IP Addresses on chosen Interface
if ($netAdapter.InterfaceIndex -notcontains $ifIndex ) {
Write-Host -ForegroundColor Red “Enter valid ifIndex!”
break
}
else {
$ifIP = Get-NetAdapter -InterfaceIndex $ifIndex | Get-NetIPAddress | where {$_.AddressFamily –eq “IPv4”} -ErrorAction stop
$ifIP | select InterfaceAlias,InterfaceIndex,Addressfamily,IpAddress
}
$IntAlias = Get-NetAdapter -InterfaceIndex $ifIndex
# Add or Remove IP address
write-host -ForegroundColor Yellow “(A)dd or (R)emove IP address ?” -NoNewline
$addrem = read-host ” ”
if ($addrem -match “a”){
Write-Host “”
Write-Host -ForegroundColor Yellow “Enter IP to add” -NoNewline
$addIP = Read-Host ” ”
New-NetIPAddress –IPAddress $addip –PrefixLength 24 –InterfaceIndex $ifIndex -ErrorAction Stop
Write-Host “”
Write-Host -ForegroundColor Cyan $addip “has been added to interface” $IntAlias.InterfaceAlias
}
if ($addrem -match “r”){
Write-Host “”
Write-Host -ForegroundColor Yellow “Enter IP to remove” -NoNewline
$remIP = Read-Host ” ”
$invalid = Get-NetIPAddress –InterfaceIndex $ifindex -AddressFamily IPv4
if ($invalid.IpAddress -notcontains $remIP){
Write-Host -ForegroundColor red $remIP “does not exist!”
break
}
else {
Get-NetIPAddress –IPAddress $remip –InterfaceIndex $ifindex | Remove-NetIPAddress -Confirm:$false
}
Write-Host “”
Write-Host -ForegroundColor Cyan $remip “has been removed from interface” $IntAlias.InterfaceAlias
}
elseif (($addrem -notmatch “a”) -and ($addrem -notmatch “r”)) {
Write-Host “”
Write-Host -ForegroundColor Red “Invalid input!”
break
}
# Display Interface IPs after Add / Remove Ip Address
Get-NetAdapter -InterfaceIndex $ifIndex | Get-NetIPAddress | where {$_.AddressFamily –eq “IPv4”} | select InterfaceAlias,InterfaceIndex,Addressfamily,IpAddress | ft -AutoSize