I am searching for $software in a list with -Match "$software". I also want to Write-Output $software to the screen.
In the case of Notepad++ I have to escape the second +.
I've tried$software=Notepad+{backtick}+ which errors with parsing "Notepad++" - Nested quantifier +.
Notepad+\\+ which doesn't error but writes Notepad+\\+ with Write-Output
The full code
$software="Notepad++"
Check-if-Installed $software
Function Check-if-Installed{ param ( [parameter(Mandatory=$true)]$software_to_check ) Write-Output "Checking if $software_to_check is installed" if ((gp HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*).DisplayName -Match "$software_to_check" -or (gp HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*).DisplayName -Match "$software_to_check") {Write-Output "$software_to_check Installed"
} else {Write-Output "$software_to_check Not Installed"}
} 5 2 Answers
You can write something like that :
Function Replace-SpecialChars { param($InputString) $SpecialChars = '[+]' $Replacement = '\+' $InputString -replace $SpecialChars,$Replacement
}
Function Check-if-Installed {
param ([parameter(Mandatory=$true)]$software_to_check)
Write-Host "Checking if $software_to_check is installed"
$OS_Architecture = $env:PROCESSOR_ARCHITECTURE
if($OS_Architecture -eq 'x86')
{ #32-bit $key = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
}
else
{ #64-bit $key = "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
}
$software = Replace-SpecialChars -InputString "$software_to_check" If((Get-ItemProperty $Key).DisplayName -Match "$software") { Write-Host "$software_to_check is Installed" -Fore Green } else { Write-Host "$software_to_check is not Installed" -Fore Red}
}
$software="Notepad++"
Check-if-Installed $software
# If you want to check within array :
$Software_Array = @("Notepad++","firefox","chrome","Bitdefender","Powershell","7-zip","winrar","winzip","Hackoo")
ForEach ($Soft in $Software_Array) { Check-if-Installed $soft
} The -Match operator uses regular expressions. Write-Output takes a literal or an expansion string.
The Escape method of the Regex class will return a string with all special characters escaped. Use this string for athe match operation.
$software = 'Notepad++'
Function Check-if-Installed{ param ( [parameter(Mandatory=$true)]$software_to_check ) $UnInstall64 = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*' $UnInstall32 = 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' Write-Output "Checking if $software_to_check is installed" $MatchString = [Regex]::Escape($software_to_check) If (( gp $UnInstall64 ).DisplayName -Match $MatchString -or ( gp $UnInstall32 ).DisplayName -Match $MatchString ) { Write-Output "$software_to_check Installed" } else { Write-Output "$software_to_check Not Installed" }
}
# In PowerShell, you have to define a fucdtion before you refence it.
Check-if-Installed $software P.S.
While wildcards are easy to use in paths. they don't seem to be the most efficeint way to deal with registry paths. Try the following code to test three different methods of getting the same data:
$UnInstall64 = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*'
$UnInstall64B = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'
Measure-Command { (gp $Uninstall64).DisplayName }
Measure-Command { (gci $Uninstall64B | gp).DisplayName }
Measure-Command { (gci $Uninstall64B).ForEach({ $_.GetValue('DisplayName') }) }May be trivial in this case, but can make a big difference when digging through something like HKCR\CLSID\....