Bulk of the script taken from and added detection of version number on top of the display name:
I’m using PS App Deploy Toolkit to detect old version of ArcGIS (9.3.1770), uninstall and eventually install 10.5.1.
The install section will look like this. If you don’t populate the variable declaration section the Handle Zero-Config MSI Installation bit will execute instead. I suggest you populate it.