Home > Mobile >  How to check with Powershell script if an IP Address is already configured
How to check with Powershell script if an IP Address is already configured

Time:09-17

I'm right now writing a powershell script, that should automatically configure an USB to Ethernet adapter on Windows 11. Sometimes the adapter has already a configured IPv6 adress and sometimes the address must be added.

I'm now struggling in how to distinguish between the two cases, so that there will be no error in case I'm trying to add a new IPv6 address, which is already there.

if (<No IPv6 address is configured for this interface alias>) {
    New-NetIPAddress –InterfaceAlias $myAdapter –IPAddress $myAddress
} else {
    Set-NetIPAddress –InterfaceAlias $myAdapter –IPAddress $myAddress
}

What code do I need in the brackets in the if clause in order to distinguish between the two cases.

CodePudding user response:

to close the question the answer was:

if ($null -eq (Get-NetIPAddress -InterfaceAlias $myAdapter -ErrorAction SilentlyContinue)) {...}
  • Related