0

Just seeing if anyone can get me over this hurdle.. tried many things and nearly have it but the wildcard is not matching. i have 2 arrays that will become many. some values have * next to them as i am using the like operator - that all works if looping through a single array btw

individually The first part of the loop will write this out as it should :

value1 value2 value3 Value4

The second part also writes out as it should like this

value1 value2 value3 value4

together they write out like this because of the loop.

value1 value2 value3 value1 value2 value3 value4 value4

The problem is even though the $_ is registering the value when used in the context below $.DisplayName -like $ it never returns a match i have also tried $.DisplayName -like "$" How can i get the wildcard to work in a multi array? It works in a single array but even tho i see the value returns in multi array it does not carry over to the actual command only the write-host?

not sure what i am missing.

$DisplayApps = @("value1", "*Value2*", "*value3*")
$Apps = @("Value4")

$MasterApps = @($DisplayApps,$Apps)

ForEach ($item in $masterapps)
{ 
  Write-host $item
    ForEach ($item in $item) 
    { Write-Host $item
      $AppVer = Get-ChildItem -Path $Path1 | Get-ItemProperty | Where-Object { $_.DisplayName -like $item }
         If($AppVer){ Write-Host "YES $item" }
    }
}
5
  • Start by not using $_ in your foreach loop. $_ is a reserved variable name. Use a non-reserved name. Commented Mar 8, 2018 at 19:12
  • Changed all $_ to $var and still no luck Commented Mar 8, 2018 at 19:15
  • @LDStewart what EBGreen was saying was that you need to change $_ to $item because you have given the variable a name. If you were doing a Foreach-Object you would use $_ Commented Mar 8, 2018 at 19:20
  • All instances of $_ above are now $item except the $_.DisplayName and still it will not find the like match Commented Mar 8, 2018 at 19:25
  • 1
    "ForEach ($item in $item)" This makes no sense. Commented Mar 8, 2018 at 19:30

1 Answer 1

1

I tried running your script locally and found a few issues.

As LDStewart pointed out in the comments above, you were misusing the $_ operator a bit.

Secondly, I can't find a property named "DisplayName" from the object Get-ItemProperty creates, however, there is a property named "Name" that contains the name of the item in the current iteration.

I updated your script with these changes and it seemed to me to produce the correct output:

$DisplayApps = @("value1", "*Value2*", "*value3*")
$Apps = @("Value4")

$MasterApps = @($DisplayApps,$Apps)

ForEach ($item in $masterapps)
{ 
  Write-host $item
    ForEach ($i in $item) 
    { Write-Host $i
      $AppVer = Get-ChildItem -Path $Path1 | Get-ItemProperty | Where-Object { $_.Name -like $i }
         If($AppVer){ Write-Host "YES $i" }
    }
}

If you want to investigate what kind of properties an object contains, you can use the "Get-Member" cmdlet to see all the properties, events and methods it contains. E.g. try running

Get-ChildItem -Path $PathToOneFile | Get-ItemProperty | Get-Member

and it will tell you all the properties available.

Sign up to request clarification or add additional context in comments.

1 Comment

just realized what the problem was - on my test machine the value4 needed to be asterisk value4 asterisk for the like operator to work... to match the registry - will mark your response correct tho. $path1 is a registry path btw

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.