-1

So I'm writing a Powershell script part of which should istall 7zip if it's not installed yet. While writing, I didn't have 7zip on my computer yet, so I was testing the code for Opera web browser (which I obviously had) with this line of code:

if((Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall* | where {$_.displayName -match "opera"}) -eq $null)

This yielded good results.But after I finally installed 7zip and used the same code (replaced "opera" with "7zip") I got nothing - meaning the Get-ItemProperty doesn't list 7zip at all (even though in control panel "Add/Remove Programs" you can see 7zip on the program list)

Any ideas how to check for 7zip being installed in some other way?

genau
  • 184
  • 1
  • 5
  • 16

3 Answers3

3

This could help:

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | ?{$_.DisplayName -like "7-Zip*"}
Micky Balladelli
  • 9,781
  • 2
  • 33
  • 31
3

What Micky is suggesting is that you need to match your query to that of your target. You are looking for 7-Zip not 7zip. If you check the same location in your registry, once you have it installed of course, you will see the key name

Mt registry. Don't tell anyone

Worth mentioning that that I have the 64-bit version installed. If I had the 32-bit version installed that logic would have failed to detect it. What you should also do is check the system architecture in one of many ways.

test-path HKLM:\Software\wow6432node\Microsoft\Windows\CurrentVersion\Uninstall

If the machine is 64-bit that will return true. WMI will also do it. Definately not the best or only way. Just want to make sure you are aware it exists. Also take note of Keith Hills answer as it is a more elegant way to handle this.

Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119
1

Rather than poke around in the registry, you could use WMI e.g.:

$prod = Get-WmiObject Win32_Product | Where {Name -match '7(-)?zip'}
if (!$prod) {
    # install 7zip
}
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Look at you thinking outside the box again. – Matt Dec 07 '14 at 22:03
  • It is bit slower than the using the registry directly but should be more bullet-proof (32-bit vs 64-bit hive, etc) I think. – Keith Hill Dec 07 '14 at 22:05
  • 2
    Actually, this should be handled with care, because querying the `Win32_Procuct` class will trigger a [reconfiguration](http://myitforum.com/cs2/blogs/gramsey/archive/2011/01/25/win32-product-is-evil.aspx) of all installed products. – Ansgar Wiechers Dec 07 '14 at 22:12
  • @AnsgarWiechers that is a good read. Also the title alone made me laugh – Matt Dec 07 '14 at 22:14