Reputation: 810
Using PowerShell, we can easily set a static IP address on an adapter where the InterfaceIndex
is known like this:
New-NetIPAddress -InterfaceIndex 17 -IPAddress 192.168.0.1 -PrefixLength 24 -DefaultGateway 192.168.0.254
We would like to be able to set the static IP address on any physical wired adapter. We can get the physical adapters like this:
Get-NetAdapter –Physical
That will return something like this:
Name InterfaceDescription ifIndex Status MacAddress LinkSpeed ---- -------------------- ------- ------ ---------- --------- Ethernet Intel(R) Ethernet Connection (5) 17 Disconnected 00-11-22-33-44-55 1 Gbps Wi-Fi Intel(R) Wireless LAN 7 Disconnected 00-11-22-33-44-56 72 Mbps
EDIT: In reading some more, it seems that since Windows 8 that the wired adapter may be configured to be statically-named "Ethernet". Since we're only targeting Windows 10, can we really just add a query to get only adapters whose name is like 'Ethernet%
?
Upvotes: 1
Views: 513
Reputation: 945
I would use
Get-NetAdapter -Physical | Where-Object { $_.PhysicalMediaType -eq "802.3" }
This seems to be a good indicator of physical ethernet only adapters, and it will get adapters that don't have "Ethernet" in the Description/DisplayName. WLAN devices will have Native 802.11 (or otherwise indicative of a Wireless Adapter).
Do Get-NetAdapter | Export-CSV C:\Path\To\CSV
to get all properties for all adapters on the system. You may find more properties to leverage in your filtering.
As for setting the IP Address on all ethernet adapters, this should work fine (although I caution that setting the same IP Address on multiple adapters may cause trouble in the however unlikely event that multiple ethernet adapters are in use simultaneously):
$ethernetAdapters = @(Get-NetAdapter -Physical | Where-Object {$_.PhysicalMediaType -eq "802.3"})
$staticIPAddress = "0.0.0.0"
$gateway = "0.0.0.0"
$prefixLength = "24"
foreach ($adapter in $ethernetAdapters) {
New-NetIPAddress -IPAddress $staticIPAddress -AddressFamily IPV4 -DefaultGateway $gateway -PrefixLength $prefixLength -InterfaceIndex $adapter.InterfaceIndex
}
Remember to replace the variables with information for your environment, and specifically the IP address you'll use for the adapters.
You may wish to figure a way to generate a new IP Address for each $adapter
, maybe in succession (*.200, *.201 etc.), with a portion that calls out to a centralized file with ip addresses, mac addresses and serial numbers for record keeping.
Upvotes: 1
Reputation: 8356
Have you tried something like
Get-NetAdapter -Physical | Where-Object { $_.Name -match "^Ethernet" }
Upvotes: 1