Add reg key through Powershell

177 Views Asked by At

I'm trying to remove and add a reg key through PS and I end up getting an error. See below. I don't know why it's causing this error.

$path1 = 'HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU'   
Remove-item -Path $HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU
$path1 = 'HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU'
New-item -Path $HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU
New-ItemProperty -Path $HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU -Name 'NoAutoUpdate' -Value 0 -PropertyType DWord

Error:

New-ItemProperty : Cannot use interface. The IDynamicPropertyCmdletProvider interface is not implemented by this
provider.
At line:1 char:1
+ New-ItemProperty -Path $HKLM\SOFTWARE\Policies\Microsoft\Windows\Wind ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotImplemented: (:) [New-ItemProperty], PSNotSupportedException
    + FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.NewItemPropertyCommand
$path1 = 'HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU'   
Remove-item -Path $HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU
$path1 = 'HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU'
New-item -Path $HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU
New-ItemProperty -Path $HKLM\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU -Name 'NoAutoUpdate' -Value 0 -PropertyType DWord

Error:

New-ItemProperty : Cannot use interface. The IDynamicPropertyCmdletProvider interface is not implemented by this
provider.
At line:1 char:1
+ New-ItemProperty -Path $HKLM\SOFTWARE\Policies\Microsoft\Windows\Wind ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotImplemented: (:) [New-ItemProperty], PSNotSupportedException
    + FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.NewItemPropertyCommand
1

There are 1 best solutions below

2
Joseph Willcoxson On
#requires -RunAsAdministrator

$path1 = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU'
Remove-Item -Path $path1 # might want to use -Force and or -Recurse
New-Item -Path $path1
New-ItemProperty -Path $path1 -Name 'NoAutoUpdate' -Value 0

What is $HKLM in your example? The path seems constant, why not use and reuse a variable and use HKLM: