Create a Custom Detection Script for Microsoft SQL Server 2017 Express (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 Microsoft SQL Server 2017 Express using PowerShell.

How to Create a Custom Detection Script for Microsoft SQL Server 2017 Express

Microsoft SQL Server 2017 Express (File Detection Method)

## Check for Microsoft SQL Server 2017 Express (File Detection Method)
$SQLExpress2017Exe = (Get-ChildItem -Path "C:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\Binn\SQLAGENT.EXE" -ErrorAction SilentlyContinue)
$SQLExpress2017Exe.FullName
$SQLExpress2017Path = $($SQLExpress2017Exe.FullName).Replace("C:\Program Files\","").Replace("C:\Program Files (x86)\","")
$ProductVersion = (Get-Item -Path "$($SQLExpress2017Exe.FullName)" -ErrorAction SilentlyContinue).VersionInfo.ProductVersion

## Create Text File with Microsoft SQL Server 2017 Express File Detection Method
$FilePath = "C:\Windows\Temp\Microsoft_SQL_Server_2017_Express_Detection_Method.txt"
New-Item -Path "$FilePath" -Force
Set-Content -Path "$FilePath" -Value "If([String](Get-Item -Path `"`$Env:ProgramFiles\$SQLExpress2017Path`" -ErrorAction SilentlyContinue).VersionInfo.ProductVersion -ge `"$ProductVersion`"){"
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 Microsoft SQL Server 2017 Express Detection Method script required to detect the current version of Microsoft SQL Server 2017 Express that is installed on the device you are running the script from.

Example:

If([String](Get-Item -Path "$Env:ProgramFiles\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\Binn\SQLAGENT.EXE" -ErrorAction SilentlyContinue).VersionInfo.ProductVersion -ge "14.0.1000.169"){
Write-Host "Installed"
Exit 0
}
else {
Exit 1
}

Microsoft SQL Server 2017 Express (Registry Detection Method)

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

## Create Text File with Microsoft SQL Server 2017 Express Registry Detection Method
$FilePath = "C:\Windows\Temp\Microsoft_SQL_Server_2017_Express_Detection_Method.txt"
New-Item -Path "$FilePath" -Force
Set-Content -Path "$FilePath" -Value "If([Version](Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$($SQLExpress2017.PSChildName)' -Name DisplayVersion -ea SilentlyContinue) -ge '$($SQLExpress2017.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 Microsoft SQL Server 2017 Express Detection Method script required to detect the current version of Microsoft SQL Server 2017 Express that is installed on the device you are running the script from.

Example:

If([Version](Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{405252DC-ADF7-4BC8-95F5-F89DE513DD62}' -Name DisplayVersion -ea SilentlyContinue) -ge '14.0.1000.169') {
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.