'Powershell substring check
I have this simple script. I just want to match the filename if they contain the substring. However it doesn't work with -like or Contains. What is the right way to do this?
$i = 0
$list1 = @('Gold','Silver')
$list2 = @('W0A-209-193-210_TVIA_Silver_20220520-100001.pdf','W0A-209-195-20_TVIA_Gold_20220520-095934.pdf')
foreach($item in $list2)
{
Write-Host $list1[$i]
if ($item -like $list1[$i])
{
Write-Host $item + $list1[$i]
}
$i++
}
Expected Output
W0A-209-193-210_TVIA_Silver_20220520-100001.pdf Silver
W0A-209-195-20_TVIA_Gold_20220520-095934.pdf Gold
Tried this code as well based on this answer but still gives no output
$list1 = @('Gold','Silver')
$list2 = @('W0A-209-193-210_TVIA_Silver_20220520-100001.pdf','W0A-209-195-20_TVIA_Gold_20220520-095934.pdf')
foreach($item in $list2)
{
foreach($name in $list1)
{
if ($item -like $name)
{
Write-Host $item + $name
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
