When automating system configurations or writing deployment scripts, checking browser versions programmatically becomes essential. While Ubuntu makes this straightforward with commands like firefox -v
, Windows and some Linux browsers require different approaches.
For Chromium-based browsers, the version information is accessible through these methods:
# Linux:
/usr/bin/google-chrome --version
# or for Chromium:
/usr/bin/chromium-browser --version
# Windows (PowerShell):
(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo.ProductVersion
The Windows version of Firefox requires registry access or file version inspection:
# Command Prompt (admin rights required):
reg query "HKLM\Software\Mozilla\Mozilla Firefox" /v CurrentVersion
# PowerShell alternative:
(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe').'(Default)').VersionInfo.ProductVersion
For IE 11 and earlier on Windows 7, use these commands:
# Command Prompt:
reg query "HKLM\Software\Microsoft\Internet Explorer" /v svcVersion
# PowerShell:
(Get-ItemProperty "HKLM:\Software\Microsoft\Internet Explorer").svcVersion
For robust version checking across multiple browsers and platforms, consider these Python approaches:
import subprocess, re
def get_chrome_version():
try:
output = subprocess.check_output(['google-chrome', '--version'])
return re.search(r'\d+\.\d+\.\d+', output.decode()).group()
except:
return "Chrome not found"
def get_firefox_version_windows():
try:
import winreg
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"Software\Mozilla\Mozilla Firefox") as key:
return winreg.QueryValueEx(key, "CurrentVersion")[0]
except:
return "Firefox not found"
For production environments, consider wrapping these checks in error handling and adding fallback methods when registry access isn't available.
When automating deployment or writing cross-platform scripts, retrieving browser versions programmatically becomes essential. Here are reliable methods for major browsers:
For Chrome/Chromium, use the --version
flag:
# Linux
google-chrome --version
# or
chromium-browser --version
# Windows (PowerShell)
& "${env:ProgramFiles(x86)}\Google\Chrome\Application\chrome.exe" --version
Windows requires the full path to Firefox executable:
# PowerShell
& "${env:ProgramFiles}\Mozilla Firefox\firefox.exe" --version
# CMD alternative
"%ProgramFiles%\Mozilla Firefox\firefox.exe" --version | more
IE doesn't support direct version flags, but we can extract version info via registry:
# PowerShell
(Get-ItemProperty "HKLM:\Software\Microsoft\Internet Explorer").svcVersion
# or for 64-bit systems:
(Get-ItemProperty "HKLM:\Software\WOW6432Node\Microsoft\Internet Explorer").svcVersion
Here's a PowerShell script that detects all installed browsers:
# Detect Chrome
try {
$chrome = & "${env:ProgramFiles(x86)}\Google\Chrome\Application\chrome.exe" --version
Write-Output "Chrome: $chrome"
} catch {
Write-Output "Chrome not detected"
}
# Detect Firefox
try {
$ff = & "${env:ProgramFiles}\Mozilla Firefox\firefox.exe" --version
Write-Output "Firefox: $ff"
} catch {
Write-Output "Firefox not detected"
}
# Detect IE
try {
$ie = (Get-ItemProperty "HKLM:\Software\Microsoft\Internet Explorer").svcVersion
Write-Output "IE Version: $ie"
} catch {
Write-Output "IE version detection failed"
}
When direct version flags aren't available, consider these approaches:
- WMI Queries (Windows):
Get-WmiObject -Class Win32_Product | Where-Object Name -like "*Chrome*"
- File Version Info: Check executable properties programmatically
- Package Managers: On Linux, use
dpkg -l | grep chromium
orrpm -qi firefox