Home > database >  How to refresh the registry in powershell after creating a new dword with new-itemproperty?
How to refresh the registry in powershell after creating a new dword with new-itemproperty?

Time:07-25

I've just created a new DWORD to be able to install the RSAT Tools without the Add-WindowsCapability failed. Error code = 0x800f0954 :

> New-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Servicing -Name RepairContentServerSource -Type dword -Value 2


RepairContentServerSource : 2
PSPath                    : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Servicing
PSParentPath              : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies
PSChildName               : Servicing
PSDrive                   : HKLM
PSProvider                : Microsoft.PowerShell.Core\Registry

The ls HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Servicing command shows nothing :

> ls HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Servicing
>

but gp HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Servicing shows both the CountryCode and my newly created itemproperty RepairContentServerSource :

> gp HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Servicing


CountryCode               : FR
RepairContentServerSource : 2
PSPath                    : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Servicing
PSParentPath              : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies
PSChildName               : Servicing
PSDrive                   : HKLM
PSProvider                : Microsoft.PowerShell.Core\Registry


>

However ls HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies shows everything including RepairContentServerSource :

> (ls HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies | Out-String -Stream | sls RepairContentServerSource).ToString()
                               RepairContentServerSource : 2
>

Is this normal ?

CodePudding user response:

Yes. Because ls is an alias for Get-ChildItem which, with registry paths, returns the subkeys of the key specified by the -path parameter, but not the values in that key.

From: Working with Registry Keys

Because registry keys are items on PowerShell drives, working with them is very similar to working with files and folders. One critical difference is that every item on a registry-based PowerShell drive is a container, just like a folder on a file system drive. However, registry entries and their associated values are properties of the items, not distinct items.

To view/display your new item property, use Get-Item(gi) instead of Get-ChildItem (gci):

PS > gi HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Servicing


    Hive: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies


Name                           Property
----                           --------
Servicing                      CountryCode : US

Bear in mind that even though gci and gi both display name/value pairs under Property, the actual Property property is a string array containing only the value names:

PS > gi HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Servicing| select Property

Property
--------
{CountryCode}

and you have to use Get-ItemProperty or Get-ItemPropertyValue to retrieve the values.

  • Related