简体   繁体   中英

Setting a new registry key and all properties in PowerShell

I am trying to test for 32 vs 64 bit and then create either one or two new keys depending upon the environment.

I would like to keep the code simple if I can and just set the Type, Name, Value, etc all at once if I can. Looking online it seems like it can be done, but whenever I try to run it, I just get prompted for a Type:

Looking for assistance / insight.

It seems pretty straightforward, here is what I am starting with:

$RegLocation1 = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_ALLOW_USER32_EXCEPTION_HANDLER_HARDENING\"
$RegLocation2 = "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_ALLOW_USER32_EXCEPTION_HANDLER_HARDENING"

If ([System.Environment]::Is32BitProcess) {
    New-Item -Path $RegLocation1 -Force
    New-ItemProperty -Path $RegLocation1 -Name "iexplore.exe" -Value 1 -ErrorAction SilentlyContinue -PropertyType DWord 
}Else {
    New-Item -Path $RegLocation1 -Force
    New-ItemProperty -Path $RegLocation1 -Name "iexplore.exe" -Value 1 -ErrorAction SilentlyContinue -PropertyType DWord 
    New-Item -Path $RegLocation2 -Force
    New-ItemProperty -Path $RegLocation2 -Name "iexplore.exe" -Value 1 -ErrorAction SilentlyContinue -PropertyType DWord
}

If you want to test for whether the current process is 32- or 64-bit, you need to test [System.Environment]::Is64Process .

If, on the other hand, you want to add the Wow6432Node key base on whether the operating system is 32- or 64-bit, you need to test [System.Environment]::Is64BitOperatingSystem .

To avoid too much code duplication, store the key paths in an array and add the Wow6432Node one based on the test, and then iterate over them in a loop:

$RegLocations = @("HKLM:\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_ALLOW_USER32_EXCEPTION_HANDLER_HARDENING")

if([System.Environment]::Is64BitProcess){
    $RegLocations += "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_ALLOW_USER32_EXCEPTION_HANDLER_HARDENING"
}

foreach($Key in $RegLocations)
{
    if(-not(Test-Path $Key)){
        New-Item -Path $Key -Force
    }
    New-ItemProperty -Path $Key -Name "iexplore.exe" -Value 1 -ErrorAction SilentlyContinue -PropertyType DWord 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM