Author Posts

August 18, 2017 at 3:45 pm

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!

August 18, 2017 at 3:56 pm

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 "
   } 
}

August 18, 2017 at 4:09 pm

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

August 18, 2017 at 4:51 pm

 $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 "
  } 
}

August 18, 2017 at 6:03 pm

$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

August 18, 2017 at 6:23 pm

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

August 18, 2017 at 6:28 pm

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

August 18, 2017 at 7:04 pm

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

August 18, 2017 at 7:13 pm

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.

August 18, 2017 at 7:54 pm

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

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

-Mike

August 18, 2017 at 10:37 pm

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.