WhiteHotLoveTiger
WhiteHotLoveTiger

Reputation: 2228

Extract Version Number from String

Using the following PowerShell command,

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
  Select-Object DisplayName, DisplayVersion |
  Select-String 'Application Name'

I get an output like this:

@{DisplayName=Application Name; DisplayVersion=52.4.1521}

If this was on Unix, I could probably figure out a sed or awk command to extract the version number, but on Windows I don't even know where to begin. How do I get that version number out as the value of a variable?

Upvotes: 1

Views: 1548

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

Get-ChildItem produces a list of objects, so you should work with the properties of those objects. Filter the list via Where-Object for the object with the display name you're looking for, then expand the DisplayVersion property:

$regpath = 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
$version = Get-ItemProperty "$regpath\*" |
           Where-Object { $_.DisplayName -eq 'Application Name' } |
           Select-Object -Expand DisplayVersion

You can also have the filter do partial matches with wildcards

... | Where-Object { $_.DisplayName -like '*partial name*' } | ...

or regular expressions

... | Where-Object { $_.DisplayName -match 'expression' } | ...

Upvotes: 2

Related Questions