Introduction
In this blog post, we will explore a PowerShell script that detects whether TeamViewer is installed on a Windows system or not. The script uses the Windows Registry to check for the presence of TeamViewer and outputs a message accordingly.
Prerequisites
The Script
The script starts by trying to detect whether TeamViewer is installed on the system. It does this by checking two registry keys: `HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*` and `HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*`. These keys contain information about installed applications, including TeamViewer.
Try {
if (Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*" | Where-Object {$_.DisplayName -Like "TeamViewer*"}) {
Write-output "TeamViewer Installer"
Exit 1
} else {
Write-output "TeamViewer not Installer"
Exit 0
}
if (Get-ItemProperty "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" | Where-Object {$_.DisplayName -Like "TeamViewer*"}) {
Write-output "TeamViewer Installer"
Exit 1
} else {
Write-output "TeamViewer not Installer"
Exit 0
}
}
How It Works
Detection Logic
The script uses the `Get-ItemProperty` cmdlet to retrieve the properties of the registry keys. The `Where-Object` cmdlet is then used to filter the results and find any entries that have a display name containing the string "TeamViewer*". If such an entry is found, the script outputs a message indicating that TeamViewer is installed.
Error Handling
The script also includes error handling using a `Try-Catch` block. If an exception occurs during execution of the script, the script will catch it and output an error message with details about the exception. The script then exits with a non-zero exit code to indicate that an error occurred.
Key Code Snippets
Catch {
$errMsg = $_.Exception.Message
write-output $errMsg
Exit 1
}
Usage Examples
To use this script, simply run it in PowerShell as an administrator. The script will output a message indicating whether TeamViewer is installed or not.
Conclusion
This PowerShell script provides a simple way to detect whether TeamViewer is installed on a Windows system or not. By using the Windows Registry and error handling, the script can accurately determine the presence of TeamViewer and provide output accordingly.