Gordon
Gordon

Reputation: 6883

Get current version number of specified program

I started with this, on a recommendation from a friend

Get-WmiObject win32_product | ft name, version

But then I found this, which gives me pause. A little research led me to this

wmic product where "Name='Revit 2018'" get name,version

Which works as far as the data gathered. And yes, I am looking for a different program in this example. in any case, once I had good info using WMIC I tried to get the data into a variable so I could get just the version number, but the data formatting is something I have never seen before. I was hoping for a simple solution, like

$object = wmic product where "Name='Revit 2018'" get name,version
$object.version

But only the result is an array with 6 items, and only one seems to be the actual data line, and that's a single line, not two properties. And, I really wonder if an old command line utility is the right answer here. If it really is the best way to do this, is there a trick to converting the raw data to something more, PowerShelly? And if it's not the best way to get this info, what is? Is that scary link real, or is Get-WmiObject win32_product actually safe? And if so, is there a way to filter on a specific name, to speed things up? And indeed, Get-WmiObject doesn't work as I was expecting, as

$object = Get-WmiObject win32_product | ft name, version
foreach ($item in $object) {
    Write-Host "$($item.version)"
}

Doesn't work as expected at all.

EDIT: This seems to be working as expected, which is progress.

$version = (Get-WmiObject win32_product -filter:"Name = 'Revit 2018'" | Select-Object -property:*).version
Write-Host "$version!"

I guess the question is really, is this a safe and consistent approach, or is there a better one?

Upvotes: 1

Views: 2117

Answers (1)

Repeat Daily
Repeat Daily

Reputation: 94

Why not use the registry?

Set-Location HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall
$app = Get-ChildItem | Where-Object { $_.GetValue("DisplayName") -match 'YourSoftware' }

$app.GetValue("DisplayVersion")

Or

Set-Location HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall
$apps = Get-ChildItem
foreach ($app in $apps) {
  $app.GetValue("DisplayName","DisplayVersion")
}

Note: You'll also need to check the SysWow64 registry location as well

HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\ 

Note: Not all items will have a display version in which case you always have the option of looking in the installation directory for the executable itself, which should have a version on it.

Upvotes: 1

Related Questions