I'm writing a script that will modify some registry settings on Windows machines.
I'm using an If to determine if the key/property exist before writing them. It should fail and write an error if it does. This chunk is working:
$currentPath = 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Ext'
$pathTest = Test-Path $currentPath
Write-Host -ForegroundColor Yellow 'Testing for: ' $currentPath
If($pathTest -ne 'True')
{
New-Item -Path $currentPath -Force | Out-Null
Write-Host -ForegroundColor Green 'Created Path: '$currentPath
}
Else{
Write-Host -ForegroundColor Red $currentPath ' already exists.'
}
Write-Host -ForegroundColor Yellow 'Disabling IE Add-On Performance Notifications (DisableAddonLoadTimePerformanceNotifications):'
If((Get-ItemProperty $currentPath -Name 'DisableAddonLoadTimePerformanceNotifications' -ea 0).'DisableAddonLoadTimePerformanceNotifications')
{
Write-Host -ForegroundColor Red 'DisableAddonLoadTimePerformanceNotifications already exists'
}
Else {
New-ItemProperty -Path $currentPath -Name DisableAddonLoadTimePerformanceNotifications -PropertyType DWord -Value '1' | Out-Null
Write-Host -ForegroundColor Green 'DisableAddonLoadTimePerformanceNotifications created and set to 1'
}
It gives me this output if the key/property exists:
Testing for: HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Ext
HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\Ext already exists.
Disabling IE Add-On Performance Notifications (DisableAddonLoadTimePerformanceNotifications):
DisableAddonLoadTimePerformanceNotifications already exists
However, this chunk does not work:
$currentPath = 'HKLM:\Software\Policies\Microsoft\Internet Explorer\Main'
$pathTest = Test-Path $currentPath
Write-Host -ForegroundColor Yellow 'Testing for: ' $currentPath
If($pathTest -ne 'True')
{
New-Item -Path $currentPath -Force | Out-Null
Write-Host -ForegroundColor Green 'Created Path: '$currentPath
}
Else{
Write-Host -ForegroundColor Red $currentPath ' already exists.'
}
If((Get-ItemProperty $currentPath -Name 'EnableAutoUpgrade' -ea 0).'EnableAutoUpgrade')
{
Write-Host -ForegroundColor Red 'EnableAutoUpgrade already exists'
}
Else{
New-ItemProperty -Path $currentPath -Name 'EnableAutoUpgrade' -PropertyType DWord -Value '0' | Out-Null
Write-Host -ForegroundColor Green 'EnableAutoUpgrade created and set to 0'
}
Output if the key/property exists:
Testing for: HKLM:\Software\Policies\Microsoft\Internet Explorer\Main
HKLM:\Software\Policies\Microsoft\Internet Explorer\Main already exists.
New-ItemProperty : The property already exists.
At C:\ps\setup\2.ps1:42 char:10
+ New-ItemProperty -Path $currentPath -Name 'EnableAutoUpgrade' -Property ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceExists: (HKEY_LOCAL_MACH...t Explorer\Main:String) [New-ItemProperty], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.NewItemPropertyCommand
EnableAutoUpgrade created and set to 0
Output of the key/property does not exist:
Testing for: HKLM:\Software\Policies\Microsoft\Internet Explorer\Main
Created Path: HKLM:\Software\Policies\Microsoft\Internet Explorer\Main
Disabling IE Auto Upgrade (EnableAutoUpgrade):
EnableAutoUpgrade created and set to 0
So, it'll create the property no problem, but can't see if it already exists.It looks to me like the If is failing on this one, but I can't figure out why. It's the same as one above it. In the full script I run a total of 14 registry entries. The ones above EnableAutoUpgrade
all work. The ones below EnableAutoUpgrade
do not and fail with the same error.
This is happening on multiple machines all running Win8.1 and PowerShell v4.
This is a real head scratcher. Any help would be appreciated.
Try this:
$currentPath = "HKLM:\Software\Policies\Microsoft\Internet Explorer\Main"
Write-Host -ForegroundColor Yellow "Testing for: " $currentPath
If(Test-Path "HKLM:\Software\Policies\Microsoft\Internet Explorer\Main")
{
Write-Host $currentPath " already exists." -ForegroundColor 'Red'
}
Else{
New-Item -Path $currentPath | Out-Null
Write-Host "Created Path: "$currentPath -ForegroundColor 'Green'
}