1

The Question

Set a static IP configuration with netsh:

netsh interface ipv4 set address name="Ethernet1" static 192.168.0.20 255.255.255.0 192.168.0.1
netsh interface ipv4 set dns name="Ethernet1" static 1.1.1.1 validate=no
netsh interface ipv4 add dns name="Ethernet1" addr=8.8.8.8 index=2 validate=no

Get the value of the Dhcp property with Get-NetIPInterface:

$Adapter = Get-NetAdapter 'Ethernet1'
($Adapter | Get-NetIPInterface -AddressFamily IPv4).Dhcp

I expect it to return Disabled but it returns Enabled.

Why does it return Enabled?


Adapter Properties in Windows UI

After executing the netsh commands as mentioned above Windows shows these settings for the Adapter:

enter image description here

They look like Dhcp is disabled.


A way to get Get-NetIPInterface to return Enabled for the property Dhcp

When I follow these steps:

  1. Open the Adapter properties in Windows as shown above
  2. Press OK (without changing anything)
  3. Close all dialogs
  4. Run Get-NetIPInterface as mentioned above again

Then for the property Dhcp it returns Disabled as expected.

I don't understand why, as I did not change anything.

Silv
  • 111
  • 4
  • This topic was also discussed here: https://social.technet.microsoft.com/Forums/lync/en-US/5122e251-2c7f-4c65-aeeb-c8d8ef77bc53/netsh-is-not-updating-dhcp-enable-status-to-no-when-network-cable-is-unplugged?forum=w7itpronetworking – Silv Mar 12 '21 at 15:15

1 Answers1

0

When using powershell to get information, why not use it to set information as well?

New-NetIPAddress -IPAddress 192.168.0.20 -InterfaceAlias 'Ethernet1' -DefaultGateway 192.168.0.1 -AddressFamily IPv4 -PrefixLength 24
Set-DnsClientServerAddress -InterfaceAlias 'Ethernet1' -serveraddresses 1.1.1.1,8.8.8.8

(Get-NetIPInterface -InterfaceAlias Wi-Fi -AddressFamily IPv4).Dhcp
Disabled

Why doesn't netsh set the same dhcp setting? I can't be sure, but it probably has something to do with netsh making changes to the older Win32_NetworkAdapterConfiguration class, while powershell's NetTCPIP module checks the newer MSFT_NetIPInterface. It might be that powershell checks some cached version and gives you the old value. Try comparing the output of these two commands?

Get-WmiObject win32_NetworkAdapterConfiguration | select DHCPEnabled,Description
Get-CimInstance -Namespace Root/StandardCimv2 -ClassName MSFT_NetIPInterface | Select Dhcp,InterfaceAlias
Cpt.Whale
  • 297
  • 1
  • 10
  • Both commands give the same output about DHCP as `Get-NetIPInterface`. If I set static IP settings with the ui the commands return that DHCP is `disabled` (as expected). If I set static IP settings with `netsh` the commands return that DHCP is still `enabled` (but I expect DHCP to be `disabled`). – Silv Mar 12 '21 at 15:19
  • @Silv If you set a static IP with powershell instead of `netsh`, does powershell still return that DHCP is enabled? And for your comment above, are you changing DHCP setttings while the interface is down/disconnected? – Cpt.Whale Mar 12 '21 at 16:15
  • I did not try setting IP settings with PowerShell. Yes I changed DHCP settings while the interface is disconnected and I read that it should work if the interface is connected, but it must be disconnected in my case. – Silv Apr 16 '21 at 11:59