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.
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”