executing of code block within foreach interation, does not execute

write-output "Function to fix MIS 140 errors entered"
write-output "------------------"
$badid = 'SB140.'
$SBDAT = Get-Content C:\Users\username\Desktop\U14187SB_testing.DAT
foreach ($line in $SBDAT)
write-output 'Processing a line'    // this line displays
if($line -contains 'SB140')           // this fails when I know that get-content file has the string in it many times
{ # pause
write-output 'found a SB140'


I have tried very hard to read on this and experiment, but being new to PS, I am not so good at its grammerI have tried very hard to read on this and experiment, but being new to PS, I am not so good at its grammer

In your if block, try replacing `-contains` with `-in`, this will search through each line and and find ‘SB140’.

Sorry, my answer is incorrect, I misread your post. @ta11ow has the correct solution.


-contains works by selecting exact matches only out of an array. -in works the same way, just semantically reversed; you can do $array -contains $item or $item -in $array, but neither will search within a string.

In order to determine whether an individual string contains some substring, use $Line.Contains($Value)

Would be easy with a switch statement.

switch -regex -file 'C:\Users\username\Desktop\U14187SB_testing.DAT'{
'SB140' {write-output 'found a SB140'}

-contains is for exact matches in arrays. I would use -match. .contains() is case sensitive.

PS C:\users\admin\foo> 'hi there sb140' -contains 'sb140'
PS C:\users\admin\foo> 'hi','there','sb140' -contains 'sb140'
PS C:\users\admin\foo> 'hi there sb140'.contains('sb140')
PS C:\users\admin\foo> 'hi there sb140'.contains('Sb140')
PS C:\users\admin\foo> 'hi there sb140' -match 'sb140'
PS C:\users\admin\foo> 'hi there sb140' -like '*sb140*'

JS, yes all correct. But the object I am searching into is piped from a xxx=get-content and then foreach $line of xxx (and I am not sure I understand what type that is for the right search or comparison command), but not searching against a interactive defined for sure string type.

if($line -match 'SB140')

Joel, didn’t work, but sure looks like it should. I added * , but didn’t with or without them, which shouldn’t be needed with a contains

$SBDAT = Get-Content C:\Users\mayres\Desktop\U14187SB_testing.DAT
foreach ($line in $SBDAT)
$count = $count + 1
'processing line ’
if ($line.Contains(‘SB140’)) { write-output ‘found a SB140’

By the way, how can I increment a counter, $count, for each line processed and display it, that is concatenate a ‘string’ with a numeric in output?

Js, even some thing so direct as if($line -macth’SB140’) is not working. I am wondering if it’s something entirely different? Though the ‘processing line’ output works. All the statements within the brackets { } after the if condition should execute, should they not?

Thanks everyone: Got it to work with this, using select-string and its required parameters nested in an if (select is successful ~ string was found)

$SBDAT = Get-Content C:\Users\B0b&Nancy\Desktop\U14187SB_testing_2.DAT
foreach ($line in $SBDAT) {
'processing line ’ + $count
if (select-string -pattern “SB140” -InputObject $sbdat ) {

write-output ‘found a SB140’ + $count

… new code to process having found the bogus “SB140” entry which I will either delete or change



Hmm. You’re double-processing the file for every line in the file. That must take a while on longer files.

Why not just Select-String the entire file rather than have a foreach loop?

-match should work. What does the input file look like? “select-string sb140 -inputobject $sbdat” will turn the whole file into one line. “if ($line | select-string sb140)” might do what you want.

“When you use the InputObject parameter to submit a collection of strings, Select-String treats the collection as a single combined string. Select-String returns the strings as a unit if it finds the search text in any string.”

Unless you want to do some delayed binding like:

if ($line | select-string -inputobject {$_})  

If the data file is unicode no bom, which can happen in windows, you can try “get-content filename -encoding unicode”.