RadCode
RadCode

Reputation: 23

PowerShell New-ItemProperty to run only if property doesn't exist

I'm trying to enable the Allow active content to run in file on My Computer with a powershell script. What I found is that PCs where this was never enabled, the registry for this setting was never created. What I want the script to do is check if it exists in the registry, if its not there... then to create it. If it does exist, then to just enable it.

Here is my attempt...

Creation

New-ItemProperty -path "HKCU:\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_LOCALMACHINE_LOCKDOWN" -Name iexplore.exe -Value "0" -PropertyType DWord

Enabling

Set-ItemProperty -path "HKCU:\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_LOCALMACHINE_LOCKDOWN" -Name iexplore.exe -Value "0" -PropertyType DWord

Using the above code... i tried using IF Else statement but just can't seem to get it right...

If(get-ItemProperty -path "HKCU:\SOFTWARE\Microsoft\Internet 
Explorer\Main\FeatureControl\FEATURE_LOCALMACHINE_LOCKDOWN" -Name iexplore.exe){
New-ItemProperty -path "HKCU:\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_LOCALMACHINE_LOCKDOWN" -Name iexplore.exe -Value "0" -PropertyType DWord -Force | Out-Null
Write-Host 'added Registry value' -f red
}
Else{ 
set-ItemProperty -path "HKCU:\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_LOCALMACHINE_LOCKDOWN" -Name iexplore.exe -Value "0"
Write-Host "Active Content is set"

Instead I get this error as well as the creation of the registry but not the correct type... which I need in DWord.

get-ItemProperty : Property iexplore.exe does not exist at path HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_LOCALMACHINE_LOCKDOWN.
At line:2 char:4
+ If(get-ItemProperty -path "HKCU:\SOFTWARE\Microsoft\Internet Explorer ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (iexplore.exe:String) [Get-ItemProperty], PSArgumentException
    + FullyQualifiedErrorId : System.Management.Automation.PSArgumentException,Microsoft.PowerShell.Commands.GetItemPropertyCommand
 
Active Content is set

Any kind of help is much appreciated

Upvotes: 1

Views: 2424

Answers (1)

Sage Pourpre
Sage Pourpre

Reputation: 10323

It look like you are doing it backward ? You do If(get-ItemProperty... THEN New-Item ELSE SET but this should be the other way around. Here's something that should work.

$RegItem = @{
    Path = 'HKCU:\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_LOCALMACHINE_LOCKDOWN'
    Name = 'iexplore.exe'
}

# Create path if missing
$Path = Get-Item -Path $RegItem.Path -ErrorAction SilentlyContinue
if ($null -eq $Path) { New-Item -Path $RegItem.Path }


if ($null -eq (Get-ItemProperty @RegItem -ErrorAction SilentlyContinue)) {
    New-ItemProperty @RegItem -Value "0" -PropertyType DWord -Force | Out-Null
    Write-Host 'added Registry value' -f red
} else {
    set-ItemProperty @RegItem -Value "0"
    Write-Host "Active Content is set"
}

Additional note

I added a section to create the FEATURE_LOCALMACHINE_LOCKDOWN if it is missing and also cleaned your code by removing all the repeat instance of the path / name.

Upvotes: 3

Related Questions