Create a Custom Detection Script for WinRAR (PowerShell)

Microsoft Endpoint Manager Configuration Manager (MEMCM / SCCM) and Microsoft Intune use Detection Rules to determine the presence of Applications & Win32 Apps. The detection rules ensure that application installations only begin to run if the application is not already installed on the device. This article will serve as an informative guide and give you a clear understanding of how to create an updated custom detection script for each new version of WinRAR using PowerShell.

How to Create a Custom Detection Script for WinRAR

WinRAR (File Detection Method)

## Check for WinRAR (File Detection Method)
$WinRARExe = (Get-ChildItem -Path "C:\Program Files\WinRAR\WinRAR.exe","C:\Program Files (x86)\WinRAR\WinRAR.exe" -ErrorAction SilentlyContinue)
$WinRARExe.FullName
$WinRARPath = $($WinRARExe.FullName).Replace("C:\Program Files\","").Replace("C:\Program Files (x86)\","")
$FileVersion = (Get-Item -Path "$($WinRARExe.FullName)" -ErrorAction SilentlyContinue).VersionInfo.FileVersion

## Create Text File with WinRAR File Detection Method
$FilePath = "C:\Windows\Temp\WinRAR_Detection_Method.txt"
New-Item -Path "$FilePath" -Force
Set-Content -Path "$FilePath" -Value "If([String](Get-Item -Path `"`$Env:ProgramFiles\$WinRARPath`",`"`${Env:ProgramFiles(x86)}\$WinRARPath`" -ErrorAction SilentlyContinue).VersionInfo.FileVersion -ge `"$FileVersion`"){"
Add-Content -Path "$FilePath" -Value "Write-Host `"Installed`""
Add-Content -Path "$FilePath" -Value "Exit 0"
Add-Content -Path "$FilePath" -Value "}"
Add-Content -Path "$FilePath" -Value "else {"
Add-Content -Path "$FilePath" -Value "Exit 1"
Add-Content -Path "$FilePath" -Value "}"
Invoke-Item $FilePath
  • Click Run Script (F5)
  • A text file will open with the WinRAR Detection Method script required to detect the current version of WinRAR that is installed on the device you are running the script from.

Example:

If([String](Get-Item -Path "$Env:ProgramFiles\WinRAR\WinRAR.exe","${Env:ProgramFiles(x86)}\WinRAR\WinRAR.exe" -ErrorAction SilentlyContinue).VersionInfo.FileVersion -ge "6.22.0"){
Write-Host "Installed"
Exit 0
}
else {
Exit 1
}

WinRAR (Registry Detection Method)

## Check for WinRAR (Registry Detection Method)
$WinRAR = Get-ChildItem -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall","HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | Get-ItemProperty | Where-Object {$_.DisplayName -match 'WinRAR' } | Select-Object -Property DisplayName, DisplayVersion, PSChildName
$WinRAR.DisplayVersion
$WinRAR.PSChildName

## Create Text File with WinRAR Registry Detection Method
$FilePath = "C:\Windows\Temp\WinRAR_Detection_Method.txt"
New-Item -Path "$FilePath" -Force
Set-Content -Path "$FilePath" -Value "If([Version](Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$($WinRAR.PSChildName)','HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\$($WinRAR.PSChildName)' -Name DisplayVersion -ea SilentlyContinue) -ge '$($WinRAR.DisplayVersion)') {"
Add-Content -Path "$FilePath" -Value "Write-Host `"Installed`""
Add-Content -Path "$FilePath" -Value "Exit 0"
Add-Content -Path "$FilePath" -Value "}"
Add-Content -Path "$FilePath" -Value "else {"
Add-Content -Path "$FilePath" -Value "Exit 1"
Add-Content -Path "$FilePath" -Value "}"
Invoke-Item $FilePath
  • Click Run Script (F5)
  • A text file will open with the WinRAR Detection Method script required to detect the current version of WinRAR that is installed on the device you are running the script from.

Example:

If([Version](Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\WinRAR archiver','HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\WinRAR archiver' -Name DisplayVersion -ea SilentlyContinue) -ge '6.22.0') {
Write-Host "Installed"
Exit 0
}
else {
Exit 1
}

Always make sure to test everything in a development environment prior to implementing anything into production. The information in this article is provided “As Is” without warranty of any kind.