I'm attempting to write a script that will be pushed to all domain computers to identify Adobe Registry Keys to and uninstall the ones for Adobe Flash via the corresponding Uninstall Strings.
I'm using the following to get any registry keys that are associate with Adobe:
Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -match "Adobe"} | Select-Object -Property DisplayName, UninstallString
The above works as needed and pulls the Adobe Keys, but I don't want to run the UninstallString for all of them, just the ones for Adobe Flash Player.
I've tried the following to try to pull just the keys that contain "Adobe Flash Player" but I keep getting errors:
Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty | Where-Object {$_.DisplayName -match "Adobe" } | Select-Object -Property DisplayName, UninstallString |
Foreach-object{if ($_.GetValue('DisplayName') -like "Adobe Flash Player"){
$uninstall = $_.GetValue('UninstallString')
cmd /c $uninstall
}
}
The key is to avoid both Get-ItemProperty
and Select-Object
, because in order to be able to call $_.GetValue(...)
in the ForEach-Object
call at the end of your pipeline, you need the original Microsoft.Win32.RegistryKey
instances output by Get-ChildItem
:
Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall,
HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
Where-Object { $_.GetValue('DisplayName') -match 'Adobe Flash Player' } |
ForEach-Object {
cmd /c $_.GetValue('UninstallString')
}
In your attempt, what $_
contained in the ForEach-Object
call was a [pscustomobject]
instance, which doesn't have a .GetValue()
method - that is what the error message is conveying.