The first part of my script seems to work fine. I am generating a list of accounts using compare-object. I am having trouble with the foreach-object. I want to exclude the line from my results if it matches any value in my $AccountsToRemove variable. Any help on what I am doing wrong would be appreciated. Final1.txt is comma delimited, $AccountsToRemove is a list of usernames.
$UserResultsPath1 = “c:\final1.txt”
$UserResultsPath2 = “c:\final2.txt”
$Compare1 = import-csv -Path $UserResultsPath1
$Compare2 = import-csv -Path $UserResultsPath2
$AccountsToRemove = Compare-Object $Compare1 $Compare2 -property SamAccountName `
| where-object {$_.SideIndicator -eq '<='} |Select-Object -Property SamAccountName -ExpandProperty SamAccountName
$file = New-Object System.IO.StreamReader -Arg $UserResultsPath1
while ($line = $file.ReadLine()) {
$AccountsToRemove | ForEach-Object {$_ -notmatch $line} | out-file "c:\finalresults.txt" -append
write-host $line
}
$file.close()