Crew Two

PuTTY Universal Uninstaller

PuTTY is a free and open-source terminal emulator, serial console and network file transfer application. It supports several network protocols, including SCP, SSH, Telnet, rlogin, and raw socket connection. It can also connect to a serial port.

Removes any version of PuTTY, whether 32 or 64-bit. A variable array is used to ensure both Program Files locations are checked. A variable is also used to determine the name of the uninstaller executable (used in older versions), as this can vary between unins000.exe and unins001.exe, depending upon how many times the software has been previously installed and removed.

Uninstall.ps1

# This script performs the installation or uninstallation of an application(s).
# Created 30/07/2019 | cru2.io
# Updated 02/12/2019
# Updated 10/03/2020
# PuTTY Universal Uninstaller


# Variables
[string]$CurrentPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
[string]$OSVersion = (Get-CimInstance Win32_OperatingSystem).version
[string]$ProcessNTAccount = (whoami)
[string]$scriptTime = (Get-Date -f "dd/MM/yyyy HH:mm:ss")
[string]$pkgName = "Simon Tatham PuTTY"
[string]$appLogFile = $env:ProgramData + '\Logs\' + $pkgName + '_Uninstall.log'
[string]$Parms = "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-"


# Functions
Function WriteLog
    {
	Param ([string]$logstring)
	Add-content $appLogFile -value $logstring
	Write-Host $logstring
    }


# Remove previous versions if exist
WriteLog "Check for and uninstall previous versions of PuTTY (32 or 64-bit)"

# EXE Uninstall
# This is run before the MSI uninstall only if the uninstall file exists
[array]$appFolders = ${env:ProgramFiles(x86)},$env:ProgramFiles
ForEach ($appFolder in $appFolders)
	{
	$appPath = $appFolder + '\' + $appName
	# This file is checked solely to log the versions found before removal
	If (Test-Path "$appPath\putty.exe")
		{$OldVersion = (Get-Item "$appPath\putty.exe").VersionInfo.FileVersion}
	
	# EXE uninstall filename ends in a variable number which must be checked
	$UninstEXE = 'unins*.exe'
	$Uninst = dir -Path $appPath -Filter $UninstEXE -Force -ErrorAction SilentlyContinue | %{$_.FullName}
	If ($Uninst -ne $null)
		{
		$appExist = 1
		ForEach ($UninstFile in $Uninst)
			{
			WriteLog " "
			WriteLog "Uninstalling $pkgName $OldVersion"
			WriteLog "$Uninst $Parms"
			Remove-Item "$appPath\putty.exe"
			Start-Process $UninstFile -ArgumentList $Parms -Wait -PassThru -NoNewWindow -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null
			Sleep 5
			Remove-Item $appPath -Recurse -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
			WriteLog "MachineName: $env:ComputerName"
			WriteLog "WindowsVersion: $OSVersion"
			WriteLog "Uninstalled by: $ProcessNTAccount"
			WriteLog "Time: $scriptTime"
			WriteLog " "
			}
		}
	}
			
If ($appExist -ne 1)
	{WriteLog "No PuTTY EXE installations detected."}


# MSI Uninstall
$UninstMSI = Get-WmiObject -Class win32_product | where { $_.Name -like "*PuTTY*"}
If ($UninstMSI -ne $null)
	{
    ForEach ($app in $UninstMSI)
		{
		WriteLog "Uninstalling $pkgName $($app.Version)"
		WriteLog "MsiExec.exe /x $($app.IdentifyingNumber) /qn REBOOT=R /l*v `"$env:ProgramData\Logs\$pkgName $($app.Version) MSI Uninstall.log`""
		Start-Process -FilePath MsiExec.exe -ArgumentList "/x $($app.IdentifyingNumber) /qn REBOOT=R /l*v `"$env:ProgramData\Logs\$pkgName $($app.Version) MSI Uninstall.log`"" -Wait -PassThru -NoNewWindow -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null
		WriteLog "MachineName: $env:ComputerName"
		WriteLog "WindowsVersion: $OSVersion"
		WriteLog "Uninstalled by: $ProcessNTAccount"
		WriteLog "Time: $scriptTime"
		WriteLog " "
		}
	}
Else
	{WriteLog "No PuTTY MSI installations detected."}

About: nick