Search code examples
powershellif-statementscriptingtry-catchregistry

-PropertyType set as DWord but when Item is created it comes out as a string


My code:

try {
    if(!(Test-Path -Path $registryPath -Value $Name)) {
        New-ItemProperty -Name test -Path HKLM:\Software\WOW6432Node\Mozilla -PropertyType DWord -Value 2
    }
}
catch {
    Set-ItemProperty -Path $registryPath -Name $Name -Value $value 
}

My Problem: the result comes out as string.

I have tried changing -PropertyType to -Type.
I have tried changing the capitilation of the word DWord.

Any help would be greatly appreciated.


Solution

  • As the docs say: You also use Set-ItemProperty to create and change registry values and data. For example, you can add a new registry entry to a key and establish or change its value.

    With Set-ItemProperty it is also possible to change the type of registry entry from say a REG_SZ (string) value to DWord, so basically all you need is:

    $registryPath = 'HKLM:\Software\WOW6432Node\Mozilla'
    $propName = 'test'
    
    Set-ItemProperty -Path $registryPath -Name $propName -Value 2 -Type DWord
    

    Of course, if you do not have permissions to create or alter something in the HKEY_LOCAL_MACHINE registry path, you will rceive an error.