locked
String concatenation gone wrong RRS feed

  • Question

  • hello all,

    I am trying to figure out how to do wildcard expansion on the below code:

    $var1 = '7-Zip 18.05 (x64 edition)';
    $OutputVariable = (gwmi win32_product -filter "Name LIKE '$var1'").Name;
    IF ($OutputVariable -eq $var1) {
    	"$var1 found." } else {
    	"$var1 not Found." }

    The above code works fine, but not very flexible.  What I envision is just having 7-Zip in the var1 variable and then having the wildcard in the -filter line.  I have tried several iterations to no avail.  Probably something simple, but I am clueless.

    Thursday, October 18, 2018 2:26 PM

Answers

  • I really liked your reply, I just had to make a small modification by changing -contains to -match

    $var1 = '7-Zip'
    $OutputVariable = (gwmi win32_product).Name
    if ($OutputVariable -match $var1) {
     "$var1 found." } else {
     "$var1 not Found." }
    I could not believe my luck when  a google search resulted in powershell string comparisons. It told me the differences between contains and match. match is what I want. Thank you!
    • Marked as answer by RandomPepsi Thursday, October 18, 2018 5:22 PM
    Thursday, October 18, 2018 5:22 PM

All replies

  • Hello,

    What about this?

    $var1 = '7-Zip 18.05 (x64 edition)'
    
    $OutputVariable = (gwmi win32_product).Name
    if ($OutputVariable -contains $var1) {
    	"$var1 found." } else {
    	"$var1 not Found." }



    • Edited by Jebisata Thursday, October 18, 2018 3:29 PM
    Thursday, October 18, 2018 3:21 PM
  • I really liked your reply, I just had to make a small modification by changing -contains to -match

    $var1 = '7-Zip'
    $OutputVariable = (gwmi win32_product).Name
    if ($OutputVariable -match $var1) {
     "$var1 found." } else {
     "$var1 not Found." }
    I could not believe my luck when  a google search resulted in powershell string comparisons. It told me the differences between contains and match. match is what I want. Thank you!
    • Marked as answer by RandomPepsi Thursday, October 18, 2018 5:22 PM
    Thursday, October 18, 2018 5:22 PM
  • Like the sql where clause, the wildcard is the percent sign.

    gwmi win32_product -filter "name like '$var1%'"


    Note that win32_product is notoriously slow, because it validates all msi's.  

    https://stackoverflow.com/questions/25083520/wmi-select-from-win32-product-takes-a-long-time



    • Edited by JS2010 Thursday, October 18, 2018 8:39 PM
    Thursday, October 18, 2018 5:29 PM