1
votes

Dealing with Information about power management setting on a network adapter, I created a loop that adds a key to disable windows from cutting power to NIC cards.

for($i=0; $i -le 20; $i++)
{
    New-Item -Path ("REGISTRY::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\" + "{0:0000}" -f $i) -Name "PnPCapabilities" -Value "24"
}

Initially it returns

Hive: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class{4D36E972-E325-11CE-BFC1-08002bE10318}\0000

Name Property
---- --------
PnPCapabilities (default) : 24

So it appears to work, but when I enter regedit, I can not see the key.

Running: Get-Itemproperty -path 'REGISTRY::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\0001\'

Also returns without the key I added. Though, running the loop again it returns with:

New-Item : A key in this path already exists. At line:3 char:5 + New-Item -Path ("REGISTRY::HKEY_LOCAL_MACHINE\SYSTEM\CurrentContr ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceExists: (Microsoft.Power...RegistryWrapper:RegistryWrapper) [New-Item], IOException + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.NewItemCommand

So I'm not exactly sure why I cant see the key I created. I'm on Windows 10 pro, if that makes a difference.

1
"{0:0000}" -f $i) should be ("{0:0000}" -f $i). You're missing the opening bracket. - Theo

1 Answers

1
votes

As commented, you missed an opening bracket in your code. Besides that, I think you should use Set-ItemProperty instead of New-Item to create or change the value of an existing property.
Also, the property you are setting is of type DWord where in your code you surround it by quotes, making it a string.

Below code should do what you want.

for($i = 0; $i -le 20; $i++) {
    # you need to double-up the opening and closing curly brackets on the GUID here, otherwise
    # the '-f' formatting operator will error out trying to find the '{0:0000}' to replace..
    $path = 'HKLM:\SYSTEM\CurrentControlSet\Control\Class\{{4D36E972-E325-11CE-BFC1-08002bE10318}}\{0:0000}' -f $i
    Set-ItemProperty -Path $path -Name 'PnPCapabilities' -Value 24 -Type 'DWord'    
}