Set Registry Key in HKEY_CLASSES_ROOT
try {
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT -ErrorAction Stop
$key = 'HKCR:\KeyPath'
$name = 'KeyName'
$val = 'Value'
if (Test-Path $key) {
Write-Output "Key exists"
$currentVal = (Get-ItemProperty $key -ErrorAction SilentlyContinue).$name
if ($currentVal -eq $val) {
Write-Output "Key and value exist"
} else {
Write-Output "Value does not match or does not exist"
New-ItemProperty -Path $key -Name $name -Value $val -PropertyType String -Force
}
} else {
Write-Output "Key and value do not exist"
New-Item -Path $key -Force
New-ItemProperty -Path $key -Name $name -Value $val -PropertyType String -Force
}
}
catch {
Write-Error "An error occurred: $_"
}
finally {
Remove-PSDrive -Name HKCR -Force -Confirm:$false
}
Set Registry Key in HKEY_USERS with Admin Privileges
New-PSDrive -Name HKU -PSProvider Registry -Root HKEY_USERS -ErrorAction SilentlyContinue
$SID = [System.Security.Principal.WindowsIdentity]::GetCurrent().User.Value
$regPath = "HKU:\$SID\KeyPath"
if (Test-Path $regPath -ErrorAction SilentlyContinue) {
Write-Host "Key exists"
} else {
Write-Host "Key does not exist"
New-Item -Path $regPath -Force -ErrorAction SilentlyContinue
}
Remove-PSDrive -Name HKU -Force -Confirm:$false -ErrorAction SilentlyContinue
Detect specific application via Registry Key
$key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*'
$name = 'DisplayName'
$val = 'Dell SupportAssist'
$matches = Get-ItemProperty -Path $key -ErrorAction SilentlyContinue | Where-Object { $_.$name -eq $val }
if ($matches) {
Write-Output "Key and value exist"
Exit 0
} else {
Write-Output "Value not exist"
Exit 1
}
Detect specific application via Registry Key with wildcard (optional install)
$key = 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
$name = 'DisplayName'
$val = 'Microsoft Visual C++ 2015*(x86)*'
$matches = Get-ChildItem -Path $key | ForEach-Object {
Get-ItemProperty $_.PSPath
} | Where-Object { $_.$name -like $val }
if ($matches) {
Write-Log -Message "VC++ 2015 x86 Redistributable (14.0.23026) already installed" -Severity 1
} else {
Write-Log -Message "VC++ 2015 x86 Redistributable 14.0.23026 is not installed, try to install it." -Severity 1
#Execute-Process -Path "$dirFiles\Disk1\ISSetupPrerequisites\{29AE0051-88F8-4ED7-A6F9-7EA37A6B11A3}\vc_redist.x86.exe" -Parameters "/install /quiet /norestart"
#Write-Log -Message "Installed VC++ 2015 x86 Redistributable 14.0.23026." -Severity 1
}