Create a Custom Detection Script for Python (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 Python using PowerShell.

How to Create a Custom Detection Script for Python

Python (File Detection Method)

## Check for Python (File Detection Method)
$PythonExe = (Get-ChildItem -Path "C:\Python*\python.exe","C:\Program Files\Python*\python.exe","C:\Program Files (x86)\Python*\python.exe" -ErrorAction SilentlyContinue)
$PythonExe.FullName
$PythonPath = $($PythonExe.FullName).Replace("C:\","").Replace("C:\Program Files\","").Replace("C:\Program Files (x86)\","")
$FileVersion = (Get-Item -Path "$($PythonExe.FullName)" -ErrorAction SilentlyContinue).VersionInfo.FileVersionRaw

## Create Text File with Python File Detection Method
$FilePath = "C:\Windows\Temp\Python_Detection_Method.txt"
New-Item -Path "$FilePath" -Force
Set-Content -Path "$FilePath" -Value "If([String](Get-Item -Path `"`$Env:HomeDrive\$PythonPath`",`"`$Env:ProgramFiles\$PythonPath`",`"`${Env:ProgramFiles(x86)}\$PythonPath`" -ErrorAction SilentlyContinue).VersionInfo.FileVersionRaw -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 Python Detection Method script required to detect the current version of Python that is installed on the device you are running the script from.

Example:

If([String](Get-Item -Path "$Env:HomeDrive\Python313\python.exe","$Env:ProgramFiles\Python313\python.exe","${Env:ProgramFiles(x86)}\Python313\python.exe" -ErrorAction SilentlyContinue).VersionInfo.FileVersionRaw -ge "3.13.150.1013"){
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.