PowerShell for Windows Admins

May 18 2012   2:00PM GMT

TCP/IP Alternative Configurations: pt III set the alternative configuration

Richard Siddaway Richard Siddaway Profile: Richard Siddaway


We have seen how to set the NIC to use DHCP to get its address. This post shows how to set the alternative configuration on the NIC. If you just want APIPA then do nothing – other wise use this script

$HKLM = 2147483650 #HKEY_LOCAL_MACHINE            
$index = 7            
$nic = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "Index=$index"            
$key = "SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\$($nic.SettingID)"            
Invoke-WmiMethod -Class StdRegprov -Name SetDWORDvalue -ArgumentList $hklm, $key, "AddressType", 2            
Invoke-WmiMethod -Class StdRegprov -Name SetMULTISTRINGvalue -ArgumentList $hklm, $key, "Alternate_$($nic.SettingID)", "ActiveConfigurations"            
Invoke-WmiMethod -Class StdRegprov -Name SetSTRINGvalue -ArgumentList $hklm, $key, "", "DhcpIpAddress"            
Invoke-WmiMethod -Class StdRegprov -Name SetSTRINGvalue -ArgumentList $hklm, $key, "", "DhcpNameServer"            
Invoke-WmiMethod -Class StdRegprov -Name SetSTRINGvalue -ArgumentList $hklm, $key, "", "DhcpSubnetMask"

Again I’m cheating by defining the NIC in terms of its Index number

The registry key is derived from the SettingID property of the NIC

We then need to set a number of registry values. The AddressType sets the alternative configuration to use our informations rather than APIPA. The ActiveCinfigurations value is set using the SettingID property of the NIC

The address, subnetmask, and names server are set.

If you look carefully at the lines where we use the SetMULTISTRINGvalue and SetSTRINGvalue methods you will notice that we give the hive, key, value and then registry value name  whereas the SetDWORDvalue method we give hive, key, value name and then value.

This is a quirk of Invoke-WmiMethod

The WMI documentation for SetMULTISTRINGvalue and SetSTRINGvalue methods state the parameters should be:

  • hive
  • registry key
  • registry value name
  • value

This order is constant across the Set* methods of the StdRegProv class –  see http://msdn.microsoft.com/en-us/library/windows/desktop/aa393600(v=vs.85).aspx

If we do some investigation

PS> ([wmiclass]”StdRegprov”).GetMethodParameters(‘SetSTRINGvalue’)
__GENUS          : 2

__CLASS          : __PARAMETERS



__RELPATH        :


__DERIVATION     : {}

__SERVER         :

__NAMESPACE      :

__PATH           :

hDefKey          : 2147483650

sSubKeyName      :

sValue           : hello

sValueName       :

This clearly shows we need to give the value before the value name.

the same holds true if we investigate using Get-CimClass in PowerShell v3

Get-CimClass -ClassName StdRegProv |

select -ExpandProperty CimClassMethods |

where Name -eq “SetStringValue” |

select -ExpandProperty Parameters


Name                                         CimType Qualifiers

—-                                         ——- ———-

hDefKey                                      UInt32 {ID, IN}

sSubKeyName                                  String {ID, IN}

sValue                                       String {ID, in}

sValueName                                   String {ID, in}

if we use Invoke-CimMethod its not to much of a problem as we have to provide the value name and value pairs as a hash table we are not relying on argument order.

I’ll post an alternative listing using the CIM cmdlets another time

2  Comments on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.
  • Lonney
    Hi Richard,

    Thanks for posting this; I'm struggling to find any other sources on setting the alternate IP settings programmatically!

    I've tried this on a Windows 10 machine, and whilst I get the same responses, the machine still only falls back to a 169.254.x.x address (APIPA enabled) or no address (APIPA disabled).

    Would you happen to know of any steps required to get Windows to switch to the alternate configuration once the parameters have been set?

    Many thanks,
    0 pointsBadges:
  • Richard Siddaway
    As far as I'm aware it just worked
    9,675 pointsBadges:

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: