Detect the presence of and remove CCleaner with PowerShell

Based on the news today, I thought I would share a couple of PowerShell code snippets to detect the presence of and silently uninstall CCleaner.

You can detect the presence of CCleaner along with the version of it you have installed via the registry.

Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Where-Object DisplayName -eq CCleaner

remove-ccleaner1b.jpg

You can use a similar command to run its uninstaller silently if it’s detected.

if (Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Where-Object DisplayName -eq CCleaner -OutVariable Results) {
& "$($Results.InstallLocation)\uninst.exe" /S
}

remove-ccleaner2b.jpg

Give it a couple of minutes and then run the first command again to verify it has been removed.

Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |
Where-Object DisplayName -eq CCleaner

remove-ccleaner3b.jpg

If PowerShell remoting is enabled on the computers in your environment, you can simply wrap these commands inside of Invoke-Command to detect and remove CCleaner from the machines in your environment. You could also consider adding the command to remove CCleaner to your login script since PowerShell remoting is not enabled by default on desktop operating systems.

The commands shown in this blog article are written to be compatible with PowerShell version 3.0 and higher. They could be made to be compatible with PowerShell version 2.0 (PowerShell version 2.0 is deprecated).


Update - September 20th, 2017

While it doesn’t appear to be possible to install the 32bit version of CCleaner on a 64bit operating system (at least not with the current version), you may also want to check the Wow64 registry settings just in case. I even went so far as to download CCleaner on a 32bit OS and install that version on a 64bit OS which still installed the 64bit version.

Get-ItemProperty -Path HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
Where-Object DisplayName -eq CCleaner

I thought about this yesterday when that registry key was referenced in a response to an unrelated Tweet of mine. When it was mentioned again today by Josh Binney, I figured it was worth adding to this blog article.


Based on one of the comments to this blog article, older versions of CCleaner may not be found using the PowerShell eq operator so you may want to consider switching to either the like or match operator. See the comments to this blog article for more details.

ยต