PowerShell Search software installed -
i attempting foreach loop search specific software install on computer using registry search. reason find 1 , not other 2 though know , can see installed. have missed.
clear-host $computers = hostname $array = @() foreach($pc in $computers){ $computername=$pc.computername #define variable hold location of installed programs $uninstallkey="software\\microsoft\\windows\\currentversion\\uninstall" #create instance of registry object , open hklm base key $reg=[microsoft.win32.registrykey]::openremotebasekey('localmachine',$computername) #drill down uninstall key using opensubkey method $regkey=$reg.opensubkey($uninstallkey) #retrieve array of string contain subkey names $subkeys=$regkey.getsubkeynames() #open each subkey , use getvalue method return required values each foreach($key in $subkeys) { $thiskey=$uninstallkey+"\\"+$key $thissubkey=$reg.opensubkey($thiskey) #if found set variable true used in report if (($thissubkey.getvalue("displayname") -like "ccleaner")) {write-host "ccleaner = true"} else {write-host = " false"} if (($thissubkey.getvalue("displayname") -like "*7-zip*")) {write-host "7zip = true"} else {write-host = " false"} if (($thissubkey.getvalue("displayname") -like "*.net framework*")) {write-host ".net = true"} else {write-host = " false"} } }
it finds dotnet , equals true, 7-zip , ccleaner installed , in place looking. have looked @ code while , can not see why.
i know have set computers hostname, change file list of computers. test @ moment.
thank , in advance.
i think of if
statements replaced switch
statement work faster , better (one call value remote registry instead of 3), plus can regex match. consider removing 3 of if
statements , replacing them with:
switch -regex ($thissubkey.getvalue("displayname")){ "ccleaner" {write-host "ccleaner = true";continue} "7-zip" {write-host "7-zip = true";continue} "\.net framework" {write-host ".net framework = true"} }
that should same thing of if
statements
edit: how 32 bit keys... well, have code things registry, tack on second part after switch modified path. better yet, let's make single list of applications installed displayname values, add ones wow6432node section list, , run whole list through switch
. assume that, since referencing computername
property in code, importing csv , looping through that? hope so, kind of based off that. loop through computers in csv computer name stored in computername
property. add 3 new properties each computer: cclean
, 7-zip
, , .net framework
. sets them $false
default. pulls software listings, , if finds of changes property $true
.
clear-host $computers = @($(new-object psobject -prop @{'computername' = $env:computername})) ##computers = import-csv 'c:\path\to\sccmverify.csv' $array = @() for($i = 0; $i -lt $computers.count;$i++){ $computername=$computers[$i].computername add-member -inputobject $computers[$i] -notepropertyname 'ccleaner' -notepropertyvalue $false add-member -inputobject $computers[$i] -notepropertyname '7-zip' -notepropertyvalue $false add-member -inputobject $computers[$i] -notepropertyname '.net framework' -notepropertyvalue $false #define variable hold location of installed programs $uninstallkey="software\\microsoft\\windows\\currentversion\\uninstall" $uninstall32key="software\\wow6432node\\microsoft\\windows\\currentversion\\uninstall" #create instance of registry object , open hklm base key $reg=[microsoft.win32.registrykey]::openremotebasekey('localmachine',$computername) #drill down uninstall key using opensubkey method $regkey=$reg.opensubkey($uninstallkey) $reg32key=$reg.opensubkey($uninstall32key) #retrieve array of string contain subkey names $subkeys=$regkey.getsubkeynames() $sub32keys=$reg32key.getsubkeynames() #open each subkey , use getvalue method return required values each, compile in list $applications = $subkeys|foreach{$reg.opensubkey("$uninstallkey\\$_").getvalue('displayname')} $applications += $sub32keys|foreach{$reg.opensubkey("$uninstall32key\\$_").getvalue('displayname')} #search applications matching software switch -regex ($applications) { "ccleaner" {write-host "ccleaner = true";$computers[$i].ccleaner = $true ;continue} "7-zip" {write-host "7-zip = true";$computers[$i].'7-zip' = $true;continue} "\.net framework" {write-host ".net framework = true";$computers[$i].'.net framework' = $true} } }