Skip to content

Instantly share code, notes, and snippets.

@straff
Last active January 15, 2019 03:27
Show Gist options
  • Save straff/ba3630a39ef9e1450f7e8bdd3bfb0b32 to your computer and use it in GitHub Desktop.
Save straff/ba3630a39ef9e1450f7e8bdd3bfb0b32 to your computer and use it in GitHub Desktop.
win_displayversion
function Get-DisplayVersion( $_displayNamePartial ) {
$_displayNameEscaped = [System.Text.RegularExpressions.Regex]::Escape($_displayNamePartial) # to handle things like 'Microsoft Visual C++ 2013 x86 Additional Runtime' - https://lazywinadmin.com/2014/09/powershell-tip-escape-regex.html
$_results = @()
$_result = (Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.DisplayName -match "$_displayNameEscaped" }) | Select-Object -Property DisplayName,DisplayVersion
if ( $_result ) { $_results += $_result }
$_result = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object { $_.DisplayName -match "$_displayNameEscaped" }) | Select-Object -Property DisplayName,DisplayVersion
if ( $_result ) { $_results += $_result }
if ( $_results.count -eq 0 ) { return (DisplayVersion-Object $false "no instances found for display name $_displayNamePartial" ) }
if ( $_results.count -gt 1 ) { return (DisplayVersion-Object $true "many instances found for display name $_displayNamePartial" ) }
return ( DisplayVersion-Object $true "found display name $_displayNamePartial" $_results[0].displayversion $_results[0].displayname)
}
function DisplayVersion-Object( $_found, $_msg, $_version=$null, $_displayname=$null ) {
return ( @{ 'found'=$_found; 'msg'=$_msg; 'version'=$_version; 'displayname'=$_displayname} )
}
<#
PS C:\Users\waltons> Get-DisplayVersion notepad
Name Value
---- -----
found True
msg found display name notepad
displayname Notepad++ (64-bit x64)
version 7.6
PS C:\Users\waltons> Get-DisplayVersion microsoft
Name Value
---- -----
found True
msg many instances found for display name microsoft
displayname
version
PS C:\Users\waltons> Get-DisplayVersion ThisDoesNotExist
Name Value
---- -----
found False
msg no instances found for display name ThisDoesNotExist
displayname
version
PS C:\Users\waltons>
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment