The open-source statistics package R can be silently installed (with this command: R-*-win.exe /VERYSILENT /NORESTART) but the R installer does not, by default, remove old versions. Make a PowerShell script to clean up old versions of R!

The following script works on a local PC or on a remote PC with WinRM. The only parameter is hostname. Leave the hostname parameter empty to run on the local PC.

This script will check if R is installed (if R is not installed the script will quit without making changes), check if the R process is running (if the R program is in use, the script will quit without making changes), and will loop through the “C:\Program Files\R” folder removing all installs except the install with the highest version number. Optionally, if you are using a package manager, you can add the commands to update to the current version of R.

#Requires -RunAsAdministrator

$Hostname = $args[0]

if (!$Hostname) {
	$Hostname = Read-Host -Prompt "Enter Hostname [$Hostname] "
}
if (!$Hostname) {
	$Target = "self"
	$Hostname = $env:computername
}

$Rprocessname = "Rgui"

if ($Target -eq "self"){

	$RDir = 'C:\Program Files\R'

	#Check for R install in Program Files.
	if (Test-Path -Path $RDir) {
		Write-Host "R is installed. Checking for running R process."
		
		#Check for running R process.
		$Rrunning = ps | findstr $Rprocessname
		if ($Rrunning -like "*$Rprocessname*") {
			Write-Host "R program is in use. Cannot upgrade at this time"
		}
		else {
			Write-Host "R program is not in use. OK to update."
			Write-Host "Checking for updates."
			
			#Add command for package manager like Chocolatey to install current R version. 
			#Or pacakge this script with the installer and add the silent install command.
		
			$RInstalledVersions = Get-ChildItem -Path "C:\Program Files\R"

			#Filter out highest numbered version before unistall! There may be a better way to do this filter.
			$array = $RInstalledVersions | Sort-Object  -Property Name -Descending
			$array[0]

			$RInstalledVersions = $RInstalledVersions | Where-Object { $_.Name -ne $array[0] }

			foreach ($Rver in $RInstalledVersions.name){
				cmd.exe /c "C:\Program Files\R\$RVer\unins000.exe" /VERYSILENT /NORESTART
				#Sometimes unins000 process hangs even after uninstall is complete. May need to kill process.
				kill -name unins000
				remove-item "C:\Program Files\R\$RVer\" -Force -Recurse -ErrorAction SilentlyContinue
			}
		}
	}
	else {
		Write-Host "R is not installed. Nothing to update."
	}
}
else {  

	$RDir = "\\$Hostname\C$\Program Files\R"

	#Check for R install in Program Files.
	if (Test-Path -Path $RDir) {

		#Check for running R process.
		$Rrunning = Invoke-Command -ComputerName $Hostname -ScriptBlock {ps | findstr $Using:Rprocessname}
		if ($Rrunning -like "*$Rprocessname*") {
			Write-Host "R program is in use. Cannot upgrade at this time"
		}

		else {
			Write-Host "R program is not in use. OK to update."
			Write-Host "Checking for updates."
			#Add command for package manager like Chocolatey to install current R version. 
			#Or pacakge this script with the installer and add the silent install command.
			}
  
			$RInstalledVersions = Invoke-Command -ComputerName $Hostname -ScriptBlock {Get-ChildItem -Path "C:\Program Files\R"}

			#Filter out highest numbered version before unistall! There may be a better way to do this filter.
			$array = $RInstalledVersions | Sort-Object  -Property Name -Descending
			$array[0]

			$RInstalledVersions = $RInstalledVersions | Where-Object { $_.Name -ne $array[0] }

			foreach ($Rver in $RInstalledVersions.name){
				Invoke-Command -ComputerName $Hostname -ScriptBlock {cmd.exe /c "C:\Program Files\R\$Using:RVer\unins000.exe" /VERYSILENT /NORESTART}
				#Sometimes unins000 process hangs even after uninstall is complete. May need to kill process.
				Invoke-Command -ComputerName $Hostname -ScriptBlock {kill -name unins000}
				Invoke-Command -ComputerName $Hostname -ScriptBlock {remove-item "C:\Program Files\R\$Using:RVer\" -Force -Recurse -ErrorAction SilentlyContinue}
			}
		}
	}
	else {
		Write-Host "R is not installed. Nothing to update."
	}
}