markh44's question (AND answer) are also still relevant 13+ years later, however, I wanted to bring some attention to a similar issue related to using WMI
to change the adapter setting, but using the "old standby" of NetSh
to do this. . .
I know O.P. is using WMI
to do this, but when I read his statement that NetSh
could do this without the network cable connected, I was completely flabbergasted because I had apparently ran into a situation where this was NOT the case and had developed a mindset that this could never be done with the network cable disconnected, so I wanted to thank josh2112 for his answer on this as well as it was the DisableDhcpOnConnect
parameter that led me to this article which explains some bugs with the NetSh
method that I encountered and could not explain nor "accurately" reproduce.
When using NetSh
to set an IP Address
to Static
this command has a faulty condition in where it will NOT set the EnableDHCP
value to a 0
when it runs! So what you end up having is this condition where NetSh
can no longer set it to DHCP
either - see screenshots below.
So - if anyone wants a way to do this without changing more than one Registry
value (and also without using the WMI
methods), you can simply change the EnableDHCP
value to 0, then use your favorite process invoker (VB.Net's
example would be Process.Start(info)
with the relevant data provided to call NetSh
via cmd.exe
) or any other method to call cmd.exe
with the necessary parameters, and it will set DHCP
appropriately for the Adapter in question, AND remove the static IPv4
entry as shown in the next screenshots: