Trying to perform If contains on word list

Hello,

I have been trying to check if a value being checked is contained in a word list.

I can get it working with just one word, but once I add the complete word list text file, it cannot find any matches.

Here is an example of my code, just for testing purposes.

$file = "Alabama","Alaska","Arizona","Arkansas","California"

$stateList = Get-Content C:\temp\StateList.txt

$data = $stateList.split(" ")

$DB = $stateList
 foreach ($Data in $DB) {
 $state = $Data.Split()[0]
}
if ($state -contains $stateList) {
Write-Host "Found State in List "
} 
# This is where I need help. $state contains California on first run and $stateList also has California, but it does not find a match. But if I hard code -contains "California", it works.
# What I'm i missing here.

Thanks for your help!

for starters your $state if is outside of your foreach loop. May try putting it inside the loop

$DB = $stateList
 foreach ($Data in $DB) 
{
 $state = $Data.Split()[0]
   if ($state -contains $stateList)
   {
    Write-Host "Found State in List "
   } 
}

Hi Thom,

I move it to inside the foreach loop, same issue. I have checked the variable and they all have the correct data.

One thing that might be doing it is with the $stateList as it contains this:
Alabama Alaska Arizona Arkansas California

So, the -contains is not parsing this the way I think it is doing it.

From what I can tell it looks like a long string value, separated by whitespace.

Thanks

 $DB = $data

I’m not really sure what you’re doing, see if $file elements are in the file statelist.txt?

$file = "Alabama","Alaska","Arizona","Arkansas","California"
$stateList = Get-Content C:\temp\StateList.txt
$data = $stateList.split(" ")
foreach ($state in $file) {
  if ($data -contains $state) {
    Write-Host "Found State in List "
  } 
}
$listA = "dog","cat","mouse","turtle","goat"

$listB = "human","goat","bear","cat","tiger"

ForEach ($l in $listA) {

if ($listB -contains $l) {write-host "matched $l"}

}

This will show what you matched on if that helps. Result is:

matched cat
matched goat

Hi JS,

I have a file with items one per line “StateList.txt” and need to check the foreach loop item, if it contains a match it will write it to the console.

The $file holds the temp data to be checked, against the stateList file.
$file = “Alabama”,“Alaska”,“Arizona”,“Arkansas”,“California”
$stateList = Get-Content C:\temp\StateList.txt

So, with your code example, if you create a stateList.txt with these states.
Alabama
Alaska
Arizona
Arkansas
California

Then, if you add say Florida to $file, but not stateList.txt it should not find it. And not write it to the console.

What I’m seeing within my editor is that $file looks like one long string
“Alabama Alaska Arizona Arkansas California Florida”

Thanks

Hi Creed,

You confirmed my issue that I just posted.

$listA = "dog","cat","mouse","turtle","goat"

$listB = "human goat bear cat tiger"

ForEach ($l in $listA) {

if ($listB -contains $l) {write-host "matched $l"}

}

will not work since the variable looks like one big string.

So I tried to split it in the listA side, but not on the listB side.

If you can try this code and look in your editor the variable will be one big string, i also thought about select-string too.

Thanks to all of you for your help,

-Mike

Why don’t you split listb? select-string or match should work too

js’s code works.

Here is the content/result of $statelist:

Alabama
Alaska
Arizona
Arkansas
California

Here is the script:

$file = "Alabama","Alaska","Arizona","Arkansas","California","Florida"
$stateList = Get-Content C:\stringtest\StateList.txt
$data = $stateList.split(" ")
foreach ($state in $file) {
  if ($data -contains $state) {
    Write-Host "Found $state in List"
  } 
}

Notice $file contains “Florida”. Here is the result:

Alabama
Alaska
Arizona
Arkansas
California
Florida

Result of the script:

Found Alabama in List
Found Alaska in List
Found Arizona in List
Found Arkansas in List
Found California in List

Weird that your .txt is not delimited(?) as it should be. Try adding a header row to it and import-csv instead of get-content.

Yes, looks to be working as i need it too.

Thanks again for taking time to help me, really appreciate it.

-Mike

Awesome! Just a heads up. I was able to remove line 3 and change your if condition to $statelist -contains $state and it still works.